Home   Best Sellers   Blogging   Coding & Design   Technology   SEO   Travel & living   Career   Videos   Tips   Calculators     
Home  »     »  JS Date Time functions - How to get Current Day, Month, Year and time using Javascript?

JS Date Time functions - How to get Current Day, Month, Year and time using Javascript?

Monday, February 24, 2014

How to get Current Day, Month, Year and time using Javascript?

There are several Date and time functions in JS to retrive current Date, Day name, Month name, year and Time

Javascript has a special class called Date, this Date class is initiated by creating a Date object and using several functions in the Date class we can access segments of Current Date and time

How to create a Date object:

// declare date object
var today = new Date();

Date and Time function in JS:

//get current day
today.getUTCDay()

// current month
today.getUTCMonth()

// get current year
today.getFullYear()

//get Hour of the day
today.getHours();

//get minute of the day
today.getMinutes();

//get second
today.getSeconds();

Here is a simple illustration of above mentioned functions


<script>
function getDateTime()
{


var daysOfWeek = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

var months=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

var today = new Date();

var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();

var date = "<b>today:</b> " + daysOfWeek[today.getUTCDay()] + ", " + months[today.getUTCMonth()] + " " + today.getUTCDate() + " "+ today.getFullYear() + " "+ hour+ " : "+ minute + " : "+ second;

return(date);

}

alert( getDateTime() );

</script>



Reference: W3Schools ( http://www.w3schools.com/jsref/jsref_obj_date.asp )



How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
JS Date Time functions - How to get Current Day, Month, Year and time using Javascript?.




Share this!




comments powered by Disqus

This Weeks 7 Popular Posts


Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   



Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »