1/29/11

JavaScript Dates

Dates contain the number of milliseconds since 1-1-1970 00:00:00 UTC.

Date Constructor
new Date()  //returns current datetime
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day [, hour, minute, second, millisecond])  //optional time-params default to 0

Params: milliseconds ms since 1-1-1970 00:00:00 UTC
        dateString   string format
        year         4 digit year
        month        0-11
        day          1-31  day of the month
        hour         0-23  hour of the day
        minute       0-59  minute segment of a time reading
        second       0-59  second segment of a time reading
        millisecond  0-999 ms segment of a time reading

Static Methods
These methods return the milliseconds since 1-1-1970
Date.now()  //use for timestamps & unique IDs, todo: minimum 15 millisecond resolution on windows?
Date.parse(datestring)
Date.UTC(year, month [, date, hrs, min, sec, ms])  //universal time

d = new Date("Jan 7, 2011")               //Fri Jan 07 2011 00:00:00 GMT-0700 (Mountain Standard Time)
d.getMonth()                              //0 (0-11)
d.getDay()                                //5 (0-6) friday
d.getDate()                               //7 (1-31)
d.getFullYear()                           //2011
d.getHours()                              //0

d = new Date("Jan 07 2011 01:02:03")      //Fri Jan 07 2011 01:02:03 GMT-0700 (Mountain Standard Time)
d.getHours()                              //1
d.getMinutes()                            //2
d.getSeconds()                            //3

d = new Date("Jan 07 2011 01:02:03:456")  //Fri Jan 07 2011 01:02:03 GMT-0700 (Mountain Standard Time)
d = new Date(2011, 0, 7, 1, 2, 3, 456)
d.getMilliseconds()                       //456
d.getTime()                               //1294387323456 milliseconds since 1-1-1970
Date.parse("01-07-2011 01:02:03:456")     //1294387323456 milliseconds since 1-1-1970
Date.UTC(2011, 0, 7, 1, 2, 3, 456)        //1294362123456

formatDate(d)                             //"1-7-2011"
formatDatePad(d)                          //"01-07-2011"
formatDateTime(d)                         //"1-7-2011 1:2:3:456"
formatDateTime(addMinutes(d, 60))         //"1-7-2011 2:2:3:456"
formatDateTime(addMinutes(d, 10))         //"1-7-2011 1:12:3:456"
formatDateTime(addMinutes(d, 1440))       //"1-8-2011 1:2:3:456"

//debug
console.log(d.getMonth());console.log(d.getDate());console.log(d.getFullYear());console.log(d.getHours());console.log(d.getMinutes());console.log(d.getSeconds());console.log(d.getMilliseconds())

//equivalent date constructors, exclude times for date only, string formats can be used with Date.parse()
new Date("01-07-2011 01:02:03")
new Date(2011, 0, 7, 1, 2, 3)
new Date("Jan 07 2011 01:02:03")
new Date("Fri Jan 7 2011 1:2:3")
new Date("Fri Jan 07 2011 01:02:03 GMT-0700")

//-------------
function addMinutes(date, minutes){  //returns date
  var result = new Date(date.getTime());
  result.setMinutes(result.getMinutes() + parseInt(minutes));
  return result;
}

function formatDate(date){  //returns string
  return (date.getMonth() + 1) + "-" + date.getDate() + "-" + date.getFullYear();
}

function formatDatePad(date){  //returns string
  return (pad2(date.getMonth() + 1)) + "-" + pad2(date.getDate()) + "-" + pad2(date.getFullYear());
}

function formatDateTime(date){  //returns string
  return (date.getMonth() + 1) + "-" + date.getDate() + "-" + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + ":" + date.getMilliseconds();
}

//http://www.electrictoolbox.com/pad-number-two-digits-javascript/
function pad2(number){  //returns string
  return (number < 10 ? "0" : "") + number;
}

Conversions
INTERVAL       MS   SECS  MINS   HOURS
1 sec        1000      1  1/60  1/3600
1 min       60000     60     1    1/60
10 min     600000    600    10   10/60
1 hour    3600000   3600    60       1
1 day    86400000  86400  1440      24  ms=24*60*60*1000

No comments:

Post a Comment