最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript -- get current date using UTC offset - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked May 23, 2016 at 19:20 stackatostackato 1,1451 gold badge20 silver badges39 bronze badges 2
  • 1 The easier way would probably be to use moment.js: momentjs./timezone – Frax Commented May 23, 2016 at 19:25
  • Australia and its territories have several time zones, ranging from +05:00 to +11:00. – RobG Commented May 24, 2016 at 4:47
Add a ment  | 

3 Answers 3

Reset to default 11

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

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论