JavaScript tutorial:
UTC method

 

Applies to: Date Object

The UTC method is use to get the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the supplied date.

Syntax

Date.UTC(year, month, day[, hours[, minutes[, seconds[,ms]]]])

The UTC method syntax has these parts:

Part

Description

year

Required. The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year.

month

Required. The month as an integer between 0 and 11 (January to December).

date

Required. The date as an integer between 1 and 31.

hours

Optional. Must be supplied if minutes is supplied. An integer from 0 to 23 (midnight to 11pm) that specifies the hour.

minutes

Optional. Must be supplied if seconds is supplied. An integer from 0 to 59 that specifies the minutes.

seconds

Optional. Must be supplied if milliseconds is supplied. An integer from 0 to 59 that specifies the seconds.

ms

Optional. An integer from 0 to 999 that specifies the milliseconds.

Return

Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the supplied date.

Example

The UTC method returns the number of milliseconds between midnight, January 1, 1970 UTC and the supplied date. This return value can be used in the setTime method and in the Date object constructor. If the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly. For example, if you specify 150 seconds, JavaScript redefines that number as two minutes and 30 seconds.

The difference between the UTC method and the Date object constructor that accepts a date is that the UTC method assumes UTC, and the Date object constructor assumes local time.

The UTC method is a static method. Therefore, a Date object does not have to be created before it can be used. The UTC method is invoked as follows:

var datestring = "November 1, 1997 10:15 AM";
Date.UTC(datestring)


Note  If year is between 0 and 99, use 1900 + year for the year.


The following example illustrates the use of the UTC method:

function DaysBetweenDateAndNow(yr, mo, dy)
{
    var d, r, t1, t2, t3;
    var MinMilli = 1000 * 60
    var HrMilli = MinMilli * 60
    var DyMilli = HrMilli * 24
    t1 = Date.UTC(yr, mo, dy)
    //Return of UTC method
    alert(t1);

    d = new Date();
    t2 = d.getTime();
    if (t2 >= t1)
    t3 = t2 - t1;
    else
    t3 = t1 - t2;
    r = Math.round(t3 / DyMilli);
    return(r);
}

document.write(DaysBetweenDateAndNow(1979, 12, 24));

To run the code above, paste it into JavaScript Editor, and click the Execute button

See also: Date Object Methods, setTime Method