Date.UTC( ) In JavaScript

The Date.UTC() method in JavaScript is used to return the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal 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[, hour[, minute[, second[, millisecond]]]]])

Parameters Used:

  1. year : To specify a year after 1900.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.

Return Value:
The Date.UTC() method returns a number representing the number of milliseconds in the given Date object since January 1, 1970, 00:00:00, universal time.

Examples for the above function are provided below.
Example 1:



Input : Date.UTC(2010, 01, 28)
Output : 1267315200000

In this example three parameters are passed in the Date.UTC() function which represent year, month and day respectively.The function returns the number of milliseconds between the date specified as parameter and midnight of January 1 1970.

Example 2:

Input : new Date(Date.UTC(2010, 01, 28))
Output : Sun Feb 28 2010 05:30:00 GMT+0530 (IST)

In this example three parameters are passed in the Date.UTC() function which represent year, month and day respectively and a date object “new date” is created.The function returns the UTC time for the passed parameters.

Codes for the above function are provided below.

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

            
    <script>
    var test = Date.UTC(2010, 01, 28);
    document.write("OUTPUT : " + test);
    </script>

    chevron_right

    
    

    OUTPUT:

    OUTPUT : 1267315200000

    In this program returning the number of milliseconds between a specified date and midnight January 1 1970.

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

            
    <script>
    var test = new Date(Date.UTC(2010, 01, 28));
    document.write("OUTPUT : " + test);
    </script>

    chevron_right

    
    

    OUTPUT:

    OUTPUT : Sun Feb 28 2010 05:30:00 GMT+0530 (IST)

    In this program creating a date object using UTC time instead of local time.



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.