JavaScript Date.UTC()

The Date.UTC () method in JavaScript returns time based on the date of January 1, 1970 and the time of 00.00.00.

Years between 0 and 99 are converted to a year in the 20th century (1900 + year); for example, 95 is converted to the year 1995. UTC time is the same as GMT time.

The UTC() method differs from the Date constructor in two ways:

  • Date.UTC() uses universal time instead of the local time.
  • Date.UTC() returns a time value as a number instead of creating a Date object.

Syntax:

Date.UTC(year, month, day, hours, minutes, seconds, millisec)

  • year : To specify a year after 1900.
  • month: To specify an integer between 0 and 11 representing the month.Other values which are allowed are :
    • -1 will represent the last month of the previous year.
    • 12 will represent the first month of the next year.
    • 13 will represent the second month of the next year.
  • day : It is an optional parameter. It is used to specify an integer between 1 and 31 representing the day of the month.Other values which are allowed are :
    • 0 will represent the last hour of the previous month.
    • -1 will represent the hour before the last hour of the previous month.
    • If the month has 31 days then 32 will represent the first day of the next month.
    • If the month has 30 days then 32 will represent the second day of the next month.
  • hour : It is an optional parameter. It is used to specify an integer between 0 and 23 representing the hours.Other values which are allowed are :
    • -1 will represent the last hour of the previous day.
    • 24 will represent the first hour of the next day.
  • minute : It is an optional parameter. It is used to specify an integer between 0 and 59 representing the minutes.Other values which are allowed are :
    • -1 will represent the last minute of the previous hour.
    • 60 will represent the first minute of the next hour.
  • second : It is an optional parameter. It is used to specify an integer between 0 and 59 representing the seconds.Other values which are allowed are :
    • -1 will represent the last second of the previous minute.
    • 60 will represent the first second of the next minute.
  • millisecond : It is an optional parameter. It is used to specify an integer between 0 and 999 representing the milliseconds.Other values which are allowed are :
    • -1 will represent the last millisecond of the previous second.
    • 1000 will represent the first millisecond of the next second.

JavaScript Date.UTC Examples

Example 1

        var now = new Date("January 02, 2019 20:00:00 GMT+0300");
        console.log(now);

output:

Wed Jan 02 2019 8:00:00 GMT + 0300 (Standard Time in Arabia)


Example 2

        let date = Date.UTC(2019,02,02)
        console.log(date)

output:

1551484800000


Example 3

        let date = new Date(Date.UTC(2019, 02, 02));
        console.log(date)

output:

Sat Mar 02 2019 03:50 AM GMT + 0300 (Arabia Standard Time)


Sources:


Browser Support

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started