Basic Usage

var myDate = new Date(DateInfo)

Here are the various possibilities for DateInfo:

  • (). The current local date and time.
  • (stampInMS). Milliseconds since midnight 01 January, 1970 UTC. The reference date is known as Unix epoch. Unix time is actually seconds since Unix epoch.
  • (dateString). Usual date strings such as "February 28, 1969", "Mon, 25 Dec 1995 13:30:00 GMT", "Mon, 25 Dec 1995 13:30:00 GMT+0430".
  • (year, month, date[, hour[, min[, sec[, ms]]]]). 1000-9999, 0-11 where 0=Jan, 1-31, 0-23, 0-59, 0-59, 0-9999.

A JavaScript date is measured in milliseconds since midnight 01 January, 1970 UTC (1970-01-01 00:00:00). A day holds 86,400,000 ms. The Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.

Try to use the FullYear variations instead of Year variations.

Methods

.getX()

These methods get/retrieve some portion of its date object. Unless otherwise noted, all are implemented JS 1.0; ECMA-262. The variations include:

  • .getDate(). 1-31. Implemented: JS 1.1; ECMA-262.
  • .getDay(). 0-6 where 0=Sunday.
  • .getFullYear(). 1000-9999. Implemented: JS 1.3; ECMA-262.
  • .getHours(). 0-23.
  • .getMilliseconds(). 0-999. Implemented: JS 1.3; ECMA-262.
  • .getMinutes(). 0-59.
  • .getMonth(). 0-11 where 0=January.
  • .getSeconds(). 0-59.
  • .getTime(). Milliseconds since midnight 01 January, 1970 UTC. Same as dtm.valueOf().
  • .getTimezoneOffset(). Minutes between local timezone and GMT, i.e. GMT - Locale. EG: If the date is CDT (GMT-05:00), then this method would return 300 (and not -300!), but GMT+0500 would return -300.
  • .getYear. Year of date minus 1900. Implemented: JS 1.0; JS 1.3 deprecated; ECMA-262.

The UTC variations temporarily converts the date object to UTC. Magically adjusts for daylight saving time. Unless otherwise noted, all are implemented JS 1.3; ECMA-262.

  • .getUTCDate(). 1-31.
  • .getUTCDay(). 0-6 where 0=Sunday.
  • .getUTCFullYear(). 1000-9999.
  • .getUTCHours(). 0-23.
  • .getUTCMilliseconds(). 0-999.
  • .getUTCMinutes(). 0-59.
  • .getUTCMonth(). 0-11 where 0=January.
  • .getUTCSeconds(). 0-59.

Note that there is no getUTCTime(), getUTCTimezoneOffset(), or getUTCYear().

.now()

Implemented: JS not; ECMA-262 not.
Static method
Returns milliseconds since midnight 01 January, 1970 UTC for the system date and time. [This isn't part of any standard but is part of many common libraries.]

var d = new Date();
d.now();    // Won't work
Date.now(); // OK

.parse(dateString)

Implemented: JS 1.0; ECMA-262.
Static method
Returns milliseconds since midnight 01 January, 1970 UTC for the given datetime string. Parse takes datetime strings like the following:

  • "Mon, 25 Dec 1995 13:30:00 GMT". http://tools.ietf.org/html/rfc822#section-5
     date-time   =  [ day "," ] date time        ; dd mm yy
                                                 ;  hh:mm:ss zzz
    
     day         =  "Mon"  / "Tue" /  "Wed"  / "Thu"
                 /  "Fri"  / "Sat" /  "Sun"
    
     date        =  1*2DIGIT month 2DIGIT        ; day month year
                                                 ;  e.g. 20 Jun 82
    
     month       =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"
                 /  "May"  /  "Jun" /  "Jul"  /  "Aug"
                 /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"
    
     time        =  hour zone                    ; ANSI and Military
    
     hour        =  2DIGIT ":" 2DIGIT [":" 2DIGIT]
                                                 ; 00:00:00 - 23:59:59
    
     zone        =  "UT"  / "GMT"                ; Universal Time
                                                 ; North American : UT
                 /  "EST" / "EDT"                ;  Eastern:  - 5/ - 4
                 /  "CST" / "CDT"                ;  Central:  - 6/ - 5
                 /  "MST" / "MDT"                ;  Mountain: - 7/ - 6
                 /  "PST" / "PDT"                ;  Pacific:  - 8/ - 7
                 /  1ALPHA                       ; Military: Z = UT;
                                                 ;  A:-1; (J not used)
                                                 ;  M:-12; N:+1; Y:+12
                 / ( ("+" / "-") 4DIGIT )        ; Local differential
                                                 ;  hours+min. (HHMM)
    
  • "12/25/1995 2:00 PM". Note that 2 digit years are assumed to be in the 1900s. Don't use 2 digit years! Variations such as P.M., pm, and p.m. are also accepted.
  • "Dec 25, 1995 2:00 PM". Variations such as P.M., pm, and p.m. are also accepted.
  • Here are some commonly accepted formats:
    • December 25, 1995
    • Mon, 25 Dec 1995 13:30:54 GMT
    • Mon, 25 Dec 1995 13:30:54 GMT+0430
    • 1995-12-25
    • 1995-12-25 13:30. Cross browser, but not in T-SQL CONVERT().
    • 1995-12-25 13:30:54
    • 1995-12-25 13:30:54.321. T-SQL native, but not in Internet Explorer.
    • 12/25
    • 12/25/1995
    • 12/25/1995 13:30
      • As of 2015-05-04, my experiments show that this format is properly consumed by Chrome, Internet Explorer, and Firefox as local time.
      • CONVERT(VARCHAR(30), MyDateTimeColumn, 101) + ' ' + CONVERT(VARCHAR(30), MyDateTimeColumn, 108) AS MyDateTimeColumn. In SQL Server. If you want to store time zone info for SQL Server 2005 and below, then use a separate column.
    • 12/25/1995 13:30:54
    • 12/25/1995 13:30:54.321
    • 1995-12-25T13:30:54.321.
      • ISO 8601 is the most cross-browser friendly. I wish ISO 8601 was but as of 2015-05-04 if no timezone offset is provided (YYYY-MM-DDTHH:MM:SS.SSS), then is the time local or UTC? Most humans (including me!), Firefox, Internet Explorer, and ISO 8601 assume local time, but Chrome and ES 5.1 assume Z time.
      • CONVERT(varchar(30), GETDATE(), 126). In SQL Server. If you want to store time zone info for SQL Server 2008 and above, then you could use the datetimeoffset data type.
// Unless you are locally in GMT, these should return different values
alert(Date.parse("Mon, 25 Dec 1995 13:30:54"));
alert(Date.parse("Mon, 25 Dec 1995 13:30:54 GMT"));

.setX()

These methods set/specify some portion of its date object. Unless otherwise noted, all are implemented JS 1.0; ECMA-262.

  • .setDate(date). 1-31.
  • .setFullYear(year[, month[, date]]). 1000-9999, 0-11, 1-31. Implemented: JS 1.3; ECMA-262.
  • .setHours(hour[, min[, sec[, ms]]]). 0-23 0-59, 0-59, 0-999. Implemented: JS 1.0; JS 1.3 added parameters for min, sec, and ms; ECMA-262.
  • .setMilliseconds(ms). 0-999. Implemented: JS 1.3; ECMA-262.
  • .setMinutes(min[, sec[, ms]]). 0-59. Implemented: JS 1.0; JS 1.3 added parameters for sec and ms; ECMA-262.
  • .setMonth(month[, date]). 0-11 where 0=January, 1-31. Implemented: JS 1.0; JS 1.3 added parameters for date; ECMA-262.
  • .setSeconds(sec[, ms]). 0-59, 0-999. Implemented: JS 1.0; JS 1.3 added parameters for ms; ECMA-262.
  • .setTime(stampInMs). Milliseconds since midnight 01 January, 1970 UTC. Same as dtm.valueOf().

Note that there is no setDay(), setTimezoneOffset(), setYear().

The UTC variations temporarily converts the date object to UTC then set the value of its date object. Magically adjusts for daylight saving time. Unless otherwise noted, all are implemented JS 1.3; ECMA-262.

  • .setUTCDate(date). 1-31.
  • .setUTCFullYear(year[, month[, date]]). 1000-9999, 0-11, 1-31. Implemented: JS 1.3; ECMA-262.
  • .setUTCHours(hour[, min[, sec[, ms]]]). 0-23 0-59, 0-59, 0-999. Implemented: JS 1.0; JS 1.3 added parameters for min, sec, and ms; ECMA-262.
  • .setUTCMilliseconds(ms). 0-999. Implemented: JS 1.3; ECMA-262.
  • .setUTCMinutes(min[, sec[, ms]]). 0-59. Implemented: JS 1.0; JS 1.3 added parameters for sec and ms; ECMA-262.
  • .setUTCMonth(month[, date]). 0-11 where 0=January, 1-31. Implemented: JS 1.0; JS 1.3 added parameters for date; ECMA-262.
  • setUTCSeconds(sec[, ms]). 0-59, 0-999. Implemented: JS 1.0; JS 1.3 added parameters for ms; ECMA-262.

Note that there is no setUTCTime().

.toY()

These methods usually translate a date object (or a portion of it) to a string of particular formats. Unless otherwise noted, all are implemented JS 1.0; ECMA-262. The variations include:

  • .toDateString(). Returns a string of the "date" part (i.e. ignores the "time" part). The format is system dependent. EG: Wed Jul 28 1993.
  • .toGMTString(). Returns a string of the date in GMT convention. The format is system dependent. EG: Mon, 18 Dec 1995 17:28:35 GMT . Implemented: JS 1.0; JS 1.3 deprecated in favor of .toUTCString(); ECMA-262.
  • .toLocaleDateString(). Returns a string of the "date" part (i.e. ignores the "time" part) in locale convention. The format is system dependent. EG: 12/18/95.
  • .toLocaleFormat(formatString). Returns a string formatted according to the given format string parameter. The format string should be like one expected by the c function strftime() [http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html] (part of the time.h header file). EG: .toLocaleFormat("%A, %B %e, %Y"); may return something like Wednesday, October 3, 2007.
  • .toLocaleString(). Returns a string of the date in locale convention. The format is system dependent. EG: 12/18/95 17:28:35 .
  • .toLocaleTimeString(). Returns a string of the "time" part (i.e. ignores the "date" part) in locale convention. The format is system dependent. EG: 17:28:35.
  • .toString(). Returns a string of the date. The format is system dependent. EG: Wed Sep 17 2008 11:46:36 GMT-0500 (Central Daylight Time). Overrides Object.toString().
  • .toTimeString(). Returns a string of the "time" part (i.e. ignores the "date" part). The format is system dependent. EG: 14:39:07 GMT-0600 (PDT).
  • .toUTCString(). Returns a string of the date in UTC convention. The format is system dependent. EG: Mon, 03 Jul 2006 21:44:38 GMT . Implemented: JS 1.3; ECMA-262.

.UTC(year, month[, date[, hour[, min[, sec[, ms]]]]])

Implemented: JS 1.0; JS 1.3 added ms parameter; ECMA-262.
Static method
1000-9999, 0-11 where 0=Jan, 1-31, 0-23, 0-59, 0-59, 0-9999. Returns milliseconds since midnight 01 January, 1970 UTC.EG:

var d = new Date();
d.UTC(2008,3);    // Won't work
Date.UTC(2008,3); // OK


GeorgeHernandez.comSome rights reserved