The toUTCString()
method is a built-in method in JavaScript that is used to convert a date object into a string that represents the date and time in Coordinated Universal Time (UTC) format. It returns a string that represents the given date and time as a human-readable string in UTC format.
The toUTCString()
method is available on the Date
object in JavaScript, and it converts the current date and time of the Date
object to a string in UTC
format.
Here is an example code snippet that demonstrates how to use the toUTCString()
method:
const date = new Date();
const utcString = date.toUTCString();
console.log(utcString);
In the above code snippet, we first create a new Date
object using the new Date()
constructor. This creates a new Date
object with the current date and time. We then call the toUTCString()
method on the date object to convert it to a UTC string. Finally, we log the UTC string to the console using console.log()
.
The output of the above code snippet will be a string that represents the current date and time in UTC format.
Here is an example output:
Mon, 10 May 2023 07:50:12 GMT
As you can see, the output string is in the format of Day, DD Month YYYY HH:MM:SS GMT
, where Day
is the day of the week, DD
is the day of the month, Month
is the name of the month, YYYY
is the year, and HH:MM:SS
is the time in hours, minutes, and seconds. The GMT
at the end of the string indicates that the time is in UTC format
.
You can also use the toUTCString()
method with a specific date and time instead of the current date and time.
Here is an example code snippet that demonstrates how to do this:
const date = new Date("2023-05-10T12:30:00");
const utcString = date.toUTCString();
console.log(utcString);
In the above code snippet, we create a new Date
object with the specific date and time of 2023-05-10T12:30:00
. We then call the toUTCString()
method on the date object to convert it to a UTC
string. Finally, we log the UTC
string to the console using console.log()
.
The output of the above code snippet will be a string that represents the specific date and time in UTC
format.
Here is an example output:
Tue, 10 May 2023 12:30:00 GMT
As you can see, the output string is in the same format as before, but it represents the specific date and time that we provided when creating the Date
object.
You can also use the toUTCString()
method with different time zones by adjusting the Date
object to the appropriate time zone.
Here is an example code snippet that demonstrates how to do this:
const date = new Date("2023-05-10T12:30:00-05:00");
const utcString = date.toUTCString();
console.log(utcString);
In the above code snippet, we create a new Date
object with the specific date and time of 2023-05-10T12:30:00-05:00
, which is in the Eastern Standard Time (EST)
time zone. We then call the toUTCString()
method on the date
object to convert it to a UTC
string. Finally, we log the UTC
string to the console using console.log()
.
The output of the above code snippet will be a string that represents the specific date and time in UTC
format.
Here is an example output:
Tue, 10 May 2023 17:30:00 GMT
As you can see, the output string represents the same date and time as before, but it has been adjusted to UTC format by subtracting 5 hours from the original time in Eastern Standard Time.
In addition to using the toUTCString()
method to convert a Date object to a UTC
string, you can also use other built-in methods in JavaScript
to format the date and time in other ways.
Here are a few examples:
const date = new Date();
// Get the year
const year = date.getUTCFullYear();
console.log(year); // Output: 2023
// Get the month (0-11)
const month = date.getUTCMonth();
console.log(month); // Output: 4 (May is month 4)
// Get the day of the month (1-31)
const day = date.getUTCDate();
console.log(day); // Output: 10
// Get the day of the week (0-6)
const weekday = date.getUTCDay();
console.log(weekday); // Output: 1 (Monday is day 1)
// Get the hours (0-23)
const hours = date.getUTCHours();
console.log(hours); // Output: 7
// Get the minutes (0-59)
const minutes = date.getUTCMinutes();
console.log(minutes); // Output: 50
// Get the seconds (0-59)
const seconds = date.getUTCSeconds();
console.log(seconds); // Output: 12
In the above code snippet, we create a new Date
object with the current date and time. We then use various built-in methods, such as getUTCFullYear()
, getUTCMonth()
, getUTCDate()
, getUTCDay()
, getUTCHours()
, getUTCMinutes()
, and getUTCSeconds()
, to extract specific components of the date and time in UTC
format. Finally, we log the extracted components to the console using console.log()
.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.