date = new Date() // creates a date set to the current date and time
Date formats
date = new Date(yy,mm,dd)
date = new Date(yy,mm,dd,hh,mm,ss)
date = new Date("Month dd, yyyy hh:mm:ss")
date = new Date(day, dd mm yyyy hh:mm:ss)
date = new Date("Month dd, yyyy")
date = new Date(milliseconds)
| Time values | Day numbers | Month numbers |
|
1 Second = 1000 milliseconds 1 Minute = 60 * 1000 1 Hour = 60 * 60 * 1000 1 Day = 24 * 60 * 60 * 1000 1 Week = 7 * 24 * 60 * 60 * 1000 |
0 = Sunday 1 = Monday 2 = Tuesday 3 = Wednesday 4 = Thursday 5 = Friday 6 = Saturday |
0 = January 1 = February 2 = March 3 = April 4 = May 5 = June 6 = July 7 = August 8 = September 9 = October 10 = November 11 = December |
Convert milliseconds into a date
date = new Date(ms) // ms = number of milliseconds since 1 January 1970 00:00:00
Convert a date into milliseconds
milliseconds = date.getTime()
milliseconds = date.parse()
milliseconds = date.UTC(yy,mm,dd,hh,mm,ss)
Adding
today = new Date().getTime()
futureDate = new Date(today + (1000 * 10) add 10 seconds
futureDate = new Date(today + (1000 * 60 * 10) add 10 minites
futureDate = new Date(today + (1000 * 60 * 60 * 10) add 10 hours
futureDate = new Date(today + (1000 * 60 * 60 * 24 * 10) add 10 days
futureDate = new Date(today + (1000 * 60 * 60 * 24 * 7 * 10) add 10 weeks
futureDate = new Date(today + (1000 * 60 * 60 * 24 * 7 * 52 * 10) add 10 years
Calculating between 2 dates
startDate = new Date(2004,02,23,12,12,12)
endDate = new Date(2004,02,01,00,00,00)
period = startDate.getTime() - endDate.getTime()
The result can be express in any one of the following ways
weeks = period / (1000 * 60 * 60 * 24 * 7) will give you 3.215496031746032 number of weeks
days = period / (1000 * 60 * 60 * 24) will give you 22.508472222222224 number of days
hours = period / (1000 * 60 * 60) will give you 540.2033333333334 number of hours
minites = period / (1000 * 60) will give you 32412.2 number of minites
seconds = period / 1000 will give you 1944732 number of seconds
or to get a collective account of weeks, days, hours, minites, and seconds the following
weeks=Math.floor(period/(60*60*1000*24*7))
days=Math.floor(period%(60*60*1000*24*7)/(1000*60*60*24))+1
hours=Math.floor( (period%(60*60*1000*24*7)%(1000*60*60*24))/(1000*60*60))
mins=Math.floor( ( (period%(60*60*1000*24*7) ) %(1000*60*60*24) ) %(1000*60*60)/(1000*60) )
secs=Math.floor( ( ( (period%(60*60*1000*24*7) ) %(1000*60*60*24) ) %(1000*60*60) )%(1000*60)/1000 )
gives you
weeks = 3, days = 2, hours = 12, minites = 12, seconds = 12
Date Object Methods
| Method | Description |
|---|---|
| Retrieving Date Object Values | |
| dateObj.getTime() | GMT milliseconds (+ or -) since zero hours GMT, January 1, 1970 |
| dateObj.getFullYear() | Local year, Cross browser |
| dateObj.getMonth() | Local month (January = 0) |
| dateObj.getDate() | Local calendar date number |
| dateObj.getDay() | Local day of the week (Sunday = 0) |
| dateObj.getHours() | Local hour of the day (0-23) |
| dateObj.getMinutes() | Local minutes of the hour (0-59) |
| dateObj.getSeconds() | Local seconds of the minute (0-59) |
Setting Date Object Values | |
| dateObj.setTime() | GMT milliseconds (+ or -) since zero hours GMT, January 1, 1970 |
| dateObj.setYear() | Local year |
| dateObj.setMonth() | Local month (January = 0) |
| dateObj.setDate() | Local calendar date number |
| dateObj.setHours() | Local hour of the day (0-23) |
| dateObj.setMinutes() | Local minutes of the hour (0-59) |
| dateObj.setSeconds() | Local seconds of the minute (0-59) |
Retrieving Client Time Zone Offset from GMT | |
| dateObj.getTimezoneOffset() | Minutes (+ to west; - to east) offset from GMT |
Converting Date Object to Strings | |
| dateObj.toGMTString() | Value as plain language GMT |
| dateObj.toLocaleString() | Value as plain language local time/date (precise format is platform dependent) |
| dateObj.toString() | Value as local string (precise format is platform dependent) |
Converting GMT Date Values to GMT Milliseconds | |
| Date.parse("IETFDateString") | Converts to GMT ms. |
| Date.UTC(yy,mm,dd,hh,mm,ss) | Converts to GMT ms. |
Note, NS and Moz
With dates prior to 2000, if you use the getYear() method for a date object that holds a 1900 date, the returned value should be the last two digits.
NS and Moz treat the year 2001 as 101.
Use the following to correct
if(year < 1000){
year += 1900
}
A year is a leap year if it is evenly divisible by 4 but not if it's evenly divisible by 100 unless it's also evenly divisible by 400.
isLeap= (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0
Summer time is now in force from the last Sunday in March until the last Sunday in October