stackato
stackato

Reputation: 1145

JavaScript -- get current date using UTC offset

How can I get the current date based on UTC Offset? For example, the UTC Offset for Australia is UTC +10:00 where it is already May 24th.

I can get UTC date and hour but can't find any Date methods that factor in UTC Offset.

Upvotes: 5

Views: 9245

Answers (3)

RobG
RobG

Reputation: 147403

Date objects have UTC methods, so if you have an offset like +10:00 you can simply apply it to the UTC time and then read the resulting UTC values. The offset can be applied as hours and minutes, or converted to a single value and applied, e.g.

/*  Return a date in yyyy-mm-dd format for the provided offset
**  @param {string} offset - offset from GMT in format +/-hh:mm
**                         - default sign is +, default offset is 00:00
**  @returns {string} date at pffset in format yyyy-mm-dd
*/
function dateAtOffset(offset){
  function z(n){return (n<10?'0':'') + n}
  var d = new Date();
  var sign = /^\-/.test(offset)? -1 : +1;
  offset = offset.match(/\d\d/g) || [0,0];
  d.setUTCMinutes(d.getUTCMinutes() + sign*(offset[0]*60 + offset[1]*1))
  return d.getUTCFullYear() + '-' + z(d.getUTCMonth() + 1) + '-' + z(d.getUTCDate());
}

var offset = '+10:00';
document.write('The date at GMT' + offset + ' is: ' + dateAtOffset(offset));

You can also adjust a date for the offset and read "local" values:

function timeAtOffset(offset) {
  var d = new Date();
  var sign = /^\-/.test(offset)? -1 : 1;
  offset = offset.match(/\d\d/g) || [0,0];
  d.setMinutes(d.getMinutes() + d.getTimezoneOffset() + sign*(offset[0]*60 + offset[1]*1));
  return d;
}

var offset = '-04:00';
document.write('Time at GMT' + offset + ' is: ' + timeAtOffset(offset))

Note however that the default toString will report the local time zone offset, not the one to which it has been adjusted, so be careful when using such an object.

Upvotes: 2

victmo
victmo

Reputation: 1216

Once you have the offset (in this case 10 hours) use this function:

function getDateWithUTCOffset(inputTzOffset){
    var now = new Date(); // get the current time

    var currentTzOffset = -now.getTimezoneOffset() / 60 // in hours, i.e. -4 in NY
    var deltaTzOffset = inputTzOffset - currentTzOffset; // timezone diff

    var nowTimestamp = now.getTime(); // get the number of milliseconds since unix epoch 
    var deltaTzOffsetMilli = deltaTzOffset * 1000 * 60 * 60; // convert hours to milliseconds (tzOffsetMilli*1000*60*60)
    var outputDate = new Date(nowTimestamp + deltaTzOffsetMilli) // your new Date object with the timezone offset applied.

    return outputDate;
}

In your case you would use:

var timeInAustralia = getDateWithUTCOffset(10);

This will return a Date object. You still need to format the date to your liking.

I agree with @Frax, Moment is a great library if you don't mind adding additional dependencies to your project.

Good luck

Upvotes: 11

Michał Perłakowski
Michał Perłakowski

Reputation: 92521

Using Moment.js and Moment Timezone it's very easy:

moment().tz("Australia/Sydney").format()

Upvotes: 2

Related Questions