从utc unixtime获取日期字符串并添加时区偏移量 [英] get date string from utc unixtime and add a timezone offset

查看:120
本文介绍了从utc unixtime获取日期字符串并添加时区偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JS中生成从UTC unix时间戳开始的Date对象(或XDate),添加时区偏移量,然后生成格式为yyyy-MM-dd HH:mm:ss的String。
现在我这样做:

  createServerRelativeTs:function(unixtimeutc,utcoffset){
var stimeUTCoffset = (isNaN(utcoffset))? 1:utcoffset;
var time = new XDate(Math.round(unixtimeutc * 1000));
var hours = time.getUTCHours()+ stimeUTCoffset;
time.setUTCHours(hours);
return time.toString(yyyy-MM-dd HH:mm:ss);
}

其中utcoffset是一个整数,unixtimeutc是一个unixtimestamp
这个代码的问题是,如果我更改我的操作系统中的时区,结果会改变!如果我将偏移值添加到时间戳,它将被忽略。如何获得独立于操作系统时区的结果?

解决方案

这是一个几个答案的组合。您需要在UTC中工作,以避免在使用非UTC方法时考虑的主机时区的影响。



所以从UNIX时间值创建一个Date,调整UTC的分钟,然后使用UTC方法格式化输出字符串作为本地。



这可能有点简单的一个库,但并不是真的有必要。 / p>

  / *给定一个Date,返回// yyyy-MM- dd HH:mm:ss格式** @param {Date} d ** @returns {string} * / function formatISOLocal(d){function z(n){return(n< 10?'0':' n} if(isNaN(d))return d.toString();返回d.getUTCFullYear()+' - '+ z(d.getUTCMonth()+ 1)+' - '+ z(d.getUTCDate())+''+ z(d.getUTCHours())+':' + z(d.getUTCMinutes())+':'+ z(d.getUTCSeconds());} / *调整提供的时区的时间值** @param {Date} d  - 日期对象** @param {string} tz-UTC偏移量为+/- HH:MM或+/- HHMM ** @returns {Date} * / function adjustForTimezone(d,tz){var sign = /^-/.test(tz)? -1:1; var b = tz.match(/ \d\d / g); //应该在这里做一些验证var offset =(b [0] * 60 + + b [1])* sign; d.setUTCMinutes(d.getUTCMinutes()+ offset); return d;} //给出了2017年1月1日的UNIX时间值00:00:00,//在// UTC + 10:00中返回等效时间的字符串// 2017-01-01T00:00:00Z秒n = 1483228800; //创建日期对象(单个数字值被视为UTC时间值)var d = new Date(n * 1000); //调整为UTC + 10:00adjustForTimezone(d,'+ 10:00 '); //格式为本地stringconsole.log(formatISOLocal(d)) 


I would like to generate in JS a Date object (or XDate) starting from a UTC unix timestamp, add a timezone offset and then generate a String in the format "yyyy-MM-dd HH:mm:ss". For now I did this:

createServerRelativeTs : function(unixtimeutc, utcoffset) {
  var stimeUTCoffset = (isNaN(utcoffset)) ? 1 : utcoffset;
  var time  = new XDate(Math.round(unixtimeutc * 1000));
  var hours = time.getUTCHours() + stimeUTCoffset;
  time.setUTCHours(hours);
  return time.toString("yyyy-MM-dd HH:mm:ss");
}

in which utcoffset is a integer number and unixtimeutc is a unixtimestamp The problem with this code is that If I change the timezone in my OS the result changes! If I add the offset value to the timestamp it gets ignored. How can I get a OS-timezone-independent result?

解决方案

This is really a combination of a few answers. You need to work in UTC to avoid the effects of the host timezone which is considered when using non-UTC methods.

So create a Date from the UNIX time value, adjust the UTC minutes for the offset, then format the output string using UTC methods as local.

This might be a bit simpler with a library, but it's not really necessary.

/* Given a Date, return a string in //yyyy-MM-dd HH:mm:ss format
**  @param {Date} d
**  @returns {string}
*/
function formatISOLocal(d) {
  function z(n){return (n<10?'0':'')+n}
  if (isNaN(d)) return d.toString();
  return d.getUTCFullYear() + '-' +
         z(d.getUTCMonth() + 1) + '-' +
         z(d.getUTCDate()) + ' ' +
         z(d.getUTCHours()) + ':' +
         z(d.getUTCMinutes()) + ':' +
         z(d.getUTCSeconds()) ;
}

/* Adjust time value for provided timezone
** @param {Date} d - date object
** @param {string} tz - UTC offset as +/-HH:MM or +/-HHMM
** @returns {Date}
*/
function adjustForTimezone(d, tz) {
  var sign = /^-/.test(tz)? -1 : 1;
  var b = tz.match(/\d\d/g); // should do some validation here
  var offset = (b[0]*60 + +b[1]) * sign;
  d.setUTCMinutes(d.getUTCMinutes() + offset);
  return d;
}

// Given a UNIX time value for 1 Jan 2017 00:00:00, 
// Return a string for the equivalent time in
// UTC+10:00

//  2017-01-01T00:00:00Z seconds
var n = 1483228800;

// Create Date object (a single number value is treated as a UTC time value)
var d = new Date(n * 1000);

// Adjust to UTC+10:00
adjustForTimezone(d, '+10:00');

// Format as local string
console.log(formatISOLocal(d))

这篇关于从utc unixtime获取日期字符串并添加时区偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆