utc date javascript code example

Example 1: javascript get utc time

const now = new Date()

// UTC Date String
now.toUTCString() // "Sun, 30 May 2021 14:59:15 GMT"

// ISO String
now.toISOString() // "2021-05-30T14:59:15.449Z"

// Unix timestamp
Math.floor(now / 1000) // 1622386878

Example 2: javascript date to utc format

new Date().toISOString()
> '2012-11-04T14:51:06.157Z'

Example 3: javascript creeate utc date

const now = new Date().toISOString();
console.log(now)

Example 4: iso date javascript

const event = new Date('05 October 2011 14:48 UTC');
console.log(event.toString());
// expected output: Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)
// (note: your timezone may vary)

console.log(event.toISOString());
// expected output: 2011-10-05T14:48:00.000Z

Tags:

Misc Example