Friday, December 5, 2014

Get datetime in javascript

new Date()
Creates a Date object with current date and time:
var now = new Date();now.format("dd/M/yy h:mm tt"); //Edit: changed TT to tt
new Date(milliseconds)
Creates a Date given the number of milliseconds passed from January 1, 1970 00:00:00, GMT+0. Note, millisecond is 1/1000 of second
// 24 hours after Jan 1, 1970
var Jan02_1970 = new Date(3600*24*1000)
alert( Jan02_1970 ) 
new Date(datestring)
If the single argument is a string. See Date.parse method for the format.
new Date(year, month [, day, hours, minutes, seconds, ms])
Note that year should have 4-digits, and month starts with 0:
new Date(2011, 0, 1, 2, 3, 4, 567) // 1 Jan 2011, 02:03:04.567 in local timezone 
To create a date given it’s components in UTC time zone, use static method Date.UTC():
Date.UTC(2011, 0, 1, 2, 3, 4, 567) // 1 Jan 2011, 02:03:04.567 in UTC 
All access to date/time components is carried out through methods.

Getter methods

getFullYear()
Get 4-digit year
getMonth()
Get month, from 0 to 11.
getDay()
Get the number of day in a week, from 0(Sunday) to 6(Saturday)
getDate()
Get day, from 1 to 31
getDay()
getHours(), getMinutes(), getSeconds(), getMilliseconds()


Example:
var d=new Date() ;
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
document.write(weekday[d.getDay()] + " ") ;
document.write(monthname[d.getMonth()] + " ");
document.write(d.getDate() + "  ");
document.write(d.getFullYear() + ", "); 

No comments:

Post a Comment