How to get DateTime with Timezone Offset (8601 format) in Javascript

What is ISO-8601 DateTime Format ?

ISO-8601 datetime strings are of the forms :

  1. Such as 2016-07-16T19:20:30+5:30 when timezone is ahead UTC
  2. Such as 2016-07-16T19:20:30-01:00 when timezone is behind UTC
  3. Such as 2016-07-16T19:20:30Z when timezone is UTC

There can also be a milliseconds field, although that is rarely used in practice.

Javascript has a toISOString method that gives a datetime in ISO-8601 format. But it gives datetime strings only in UTC format, YYYY-MM-DDTHH:mm:ss.sssZ. It does not give a timezone offset in hours and minutes. To get this, you need a bit of custom code.

Demo

1) Getting the Timezone Offset in Hours & Minutes

Javascript has a getTimezoneOffset method which gives timezone difference, in minutes, from current local time to UTC. We can convert this into hours and minutes, and get the timezone offset in hours and minutes.

Note that getTimezoneOffset returns an offset which is positive if the local timezone is behind UTC and negative if it is ahead. So we must add an opposite sign (+ or -) to the offset.

var timezone_offset_min = new Date().getTimezoneOffset(),
	offset_hrs = parseInt(Math.abs(timezone_offset_min/60)),
	offset_min = Math.abs(timezone_offset_min%60),
	timezone_standard;

if(offset_hrs < 10)
	offset_hrs = '0' + offset_hrs;

if(offset_min < 10)
	offset_min = '0' + offset_min;

// Add an opposite sign to the offset
// If offset is 0, it means timezone is UTC
if(timezone_offset_min < 0)
	timezone_standard = '+' + offset_hrs + ':' + offset_min;
else if(timezone_offset_min > 0)
	timezone_standard = '-' + offset_hrs + ':' + offset_min;
else if(timezone_offset_min == 0)
	timezone_standard = 'Z';

// Timezone difference in hours and minutes
// String such as +5:30 or -6:00 or Z
console.log(timezone_standard); 

2) Getting DateTime

You can get current datetime using the Date object.

var dt = new Date(),
	current_date = dt.getDate(),
	current_month = dt.getMonth() + 1,
	current_year = dt.getFullYear(),
	current_hrs = dt.getHours(),
	current_mins = dt.getMinutes(),
	current_secs = dt.getSeconds(),
	current_datetime;

// Add 0 before date, month, hrs, mins or secs if they are less than 0
current_date = current_date < 10 ? '0' + current_date : current_date;
current_month = current_month < 10 ? '0' + current_month : current_month;
current_hrs = current_hrs < 10 ? '0' + current_hrs : current_hrs;
current_mins = current_mins < 10 ? '0' + current_mins : current_mins;
current_secs = current_secs < 10 ? '0' + current_secs : current_secs;

// Current datetime
// String such as 2016-07-16T19:20:30
current_datetime = current_year + '-' + current_month + '-' + current_date + 'T' + current_hrs + ':' + current_mins + ':' + current_secs;

If you are need of milliseconds also, you can use getMilliseconds method.

If you want a datetime string set to some other date & time (not the current time), pass an appopriate parameter to the Date object. See Several ways to create a Date object for more.

3) Combine both DateTime and Timezone Offset

Concatenate datetime and timezone offset to get an ISO-8601 datetime string.

console.log(current_datetime + timezone_standard);
I'm available for freelancing
If you've liked this article, you'll probably like my code too.

From short code snippets to complete web applications, I code in Javascript & PHP
Low Prices from $20, Fast Delivery Time
I'm available for freelancing
If you've liked this article, you'll probably like my code too.

From short code snippets to complete web applications, I code in Javascript & PHP
Low Prices from $20, Fast Delivery Time