Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
communitycreator

What is toUTCString() in JavaScript?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

toUTCString() is a JavaScript date object method that converts a date to a string using the UTCUniversal Time Coordinated time zone.

Syntax

dateObj.toUTCString()

Parameters

No parameters are passed to this method. toUTCString() only converts the contents of a Date() constructor object.

Return value

toUTCString() returns a string converted according to UTC. The value is in the form of Www, dd Mmm yyyy hh:mm:ss GMT.

Format Description
Www Day of the week
dd Day of the month
Mmm Month
yyyy Year
hh Hour
mm Minute
ss Seconds

Example

In the example below, we will take a look at Date constructor objects with their contents and see how toUTCSTring() converts them.

// Data objects with their contents
let dateobj1 = new Date();
let dateobj2 = new Date('October 15, 1996 05:35:32');
let dateobj3 = new Date('4, 5, 6');
let dateobj4 = new Date("2021-10-25");
let dateobj5 = new Date("2021/10/25");
// Printing UTC converted Date strings
console.log(dateobj1.toUTCString())
console.log(dateobj2.toUTCString())
console.log(dateobj3.toUTCString())
console.log(dateobj4.toUTCString())
console.log(dateobj5.toUTCString())

Note: The toUTCString() method returns invalid date if the initial content of the Date object is incorrect.

See the example below:

// Data objects with incorrect date strings
let dateobj1 = new Date('7, 8, 3, 4');
let dateobj2 = new Date("2021-10-2021");
let dateobj3 = new Date("2021/10/2021");
console.log(dateobj1.toUTCString())
console.log(dateobj2.toUTCString())
console.log(dateobj3.toUTCString())

RELATED TAGS

javascript
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring