JavaScript Date UTC() Method

21 Oct, 2021
Below is the example of Date UTC() method.
Example:<script>   var gfg = Date.UTC(2020, 07, 03);   document.write("Output : " + gfg);</script>
<script>   var gfg = Date.UTC(2020, 07, 03);   document.write("Output : " + gfg);</script>
Output:Output : 1596412800000
Output : 1596412800000
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: This method accepts seven parameters as mentioned above and described below:
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.
-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.
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.
-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.
-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.
-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.
-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.
Below Examples illustrate the method in JavaScript:
Example 1: In this example three parameters are passed in the Date.UTC() method represents the year, month, and day respectively. The method returns the number of milliseconds between the date specified as parameter and midnight of January 1, 1970.Date.UTC(2010, 01, 28)Output:1267315200000
Date.UTC(2010, 01, 28)
Output:
1267315200000
Example 2: In this example three parameters are passed in the Date.UTC() method which represent year, month, and day respectively, and a data object “new date” is created. The method returns the UTC time for the passed parameters.new Date(Date.UTC(2010, 01, 28))Output:Sun Feb 28 2010 05:30:00 GMT+0530 (IST)
new Date(Date.UTC(2010, 01, 28))
Output:
Sun Feb 28 2010 05:30:00 GMT+0530 (IST)
More codes for the above method are as follows:
Program 1: In this program returning the number of milliseconds between a specified date and midnight January 1, 1970.
<script>   var test = Date.UTC(2010, 01, 28);   document.write("Output : " + test);</script>
Output:
Output : 1267315200000
Program 2: In this program creating a date object using UTC time instead of local time.
<script>var test = new Date(Date.UTC(2010, 01, 28));document.write("Output : " + test);</script>
Output:
Output : Sun Feb 28 2010 05:30:00 GMT+0530 (IST)
Supported Browsers: The browsers supported by JavaScript Date UTC() Method are listed below:
Google Chrome 1 and above
Edge 12 and above
Firefox 1 and above
Internet Explorer 3 and above
Opera 3 and above
Safari 1 and above
ysachin2314
date-time-program
javascript-date
JavaScript-Methods
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
How to append HTML code to a div using JavaScript ?
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?