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 )
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 )
Comments
Post a Comment