在JavaScript中将时间表示为CST-date-fns [英] Express time as CST in javascript - date-fns

查看:97
本文介绍了在JavaScript中将时间表示为CST-date-fns的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用date-fns格式化日期

如果我传递以Z结尾的日期,它将知道它是UTC,因此我可以 format(date,"yyyy-MM-dd"),它将在本地计算机中时间.

如果我要在本地计算机时间中表示的日期原本是CST,那么是否要在末尾添加一些内容而不是Z,那么格式化功能会将其理解为CST日期?

抱歉,这是一个不好的问题

是否可以在date-fns中执行 zonedTimeToUtc(myDate,'UTC-6')?(而不是使用时区 name )

解决方案

如果您始终希望使用date-fns将其解析为CST(美国中部标准时间),则可以包含date-fns-tz并进行设置解析时的时区(我假设ISO 8601宽松格式没有时区).请注意,为避免DST,您必须选择一个全年使用UTC-6的位置,例如加拿大/萨斯喀彻温省.

 //设置var {parse} = require('date-fns');var {zonedTimeToUtc,utcToZonedTime,format} = require('date-fns-tz');//使用位置解析偏移量let loc ='加拿大/萨斯喀彻温省';让s ='2020-08-14 13:05:52';让fIn ='yyyy-MM-dd HH:mm:ss';让utcDate = zonedTimeToUtc(s,loc);//显示本地等效项console.log(utcDate); 

这使您有点受萨斯喀彻温省管理员的摆布,他们可能会更改偏移量或引入DST.一种替代方法是将所需的确切偏移量附加到时间戳上,并将其包括在解析令牌中:

 //使用字符串作为偏移量进行解析令tz ='-06';令utcDate2 = parse(s +''+ tz,fIn +'X',new Date());//显示本地等效项,应与上面相同console.log(utcDate2); 

第二种方法的优点是它不需要date-fns-tz,并且您不必担心萨斯喀彻温省(或其他任何IANA位置)的偏移量的历史性或将来的变化.

显然,正在开发一个UTC模块,该模块将允许设置-6之类的特定偏移量,而不是使用IANA位置(找不到指向该注释atm的链接).

这时该字符串已解析为GMT-6,但仍只是一个简单的Date(即,只是一个时间值,不知道与原始字符串相关联的时区).

一旦有了日期,就可以将其显示为CST以便输出.要在调用 format 的偏移中使用IANA位置,您必须使用date-fns-tz格式,而不是纯date-fns格式,否则它将仅使用主机系统偏移.

请注意,格式调用中的值只是设置要用于偏移量字符串的值,它对实际的日期和时间没有任何作用, utcToZonedTime .

 //调整为CST让dCST = utcToZonedTime(utcDate2,loc);//格式化字符串:让fOut1 ='yyyy-MM-dd HH:mm:ss XXX';//-0600令fOut2 ='yyyy-MM-dd HH:mm:ss z';//CST//使用位置进行格式化console.log(format(dCST,fOut1,{timeZone:loc}));console.log(format(dCST,fOut2,{timeZone:loc})); 

我更喜欢-0600版本,因为它避免了是否遵守DST(实际上就是代码在做什么)的问题.另外,在"z"中,版本可能会获得偏移量或时区名称(可能取决于主机的默认语言和位置,我认为这是使用 Intl.DateTimeFormat 的date-fns-tz的怪癖).

您还可以使用以下格式字符串手动添加时区:

  let fOut ='yyyy-MM-dd HH:mm:ss \'-0600 \''; 

这将产生如下输出:

 "2020-08-14 13:05:52 GMT-0600" 

我认为没有任何方法可以设置特定的偏移量,例如"-0600"对于解析 npm.runkit.com 上运行.当前date-fns版本没有CDN,因此代码无法在此处运行.

  var {parse} = require('date-fns');var {zonedTimeToUtc,utcToZonedTime,format} = require('date-fns-tz');//使用位置解析偏移量let loc ='加拿大/萨斯喀彻温省';让s ='2020-08-14 13:05:52';让fIn ='yyyy-MM-dd HH:mm:ss';让utcDate = zonedTimeToUtc(s,loc);//显示本地等效项console.log(utcDate);//使用字符串解析偏移量令tz ='-06';令utcDate2 = parse(s +''+ tz,fIn +'X',new Date());//显示本地等效项,应与上面相同console.log(utcDate2);//使用位置进行格式化:让fOut1 ='yyyy-MM-dd HH:mm:ss XXX';//-0600令fOut2 ='yyyy-MM-dd HH:mm:ss z';//CST让dCST = utcToZonedTime(utcDate2,loc);console.log(format(dCST,fOut1,{timeZone:loc}));console.log(format(dCST,fOut2,{timeZone:loc})); 

I am using date-fns to format dates

If I pass a date that ends in Z, it knows that it is UTC, and I can format(date, "yyyy-MM-dd") and it will be in local computer time.

If the date I want to express in local computer time is originally CST, is there something to add at the end instead of the Z, that will be understood by the format function as a CST date?

Sorry if this is a bad question

Edit: is there a way to do zonedTimeToUtc(myDate, 'UTC-6') in date-fns? (instead of using a time zone name)

解决方案

If you have a string that you always want parsed as CST (US central standard time) using date-fns, you can include date-fns-tz and set the timezone when parsing (I've assumed an ISO 8601 loose format without the timezone). Note that to avoid DST, you have to pick a location that is UTC-6 all year round, e.g. Canada/Saskatchewan.

// Setup
var {parse} = require('date-fns');
var {zonedTimeToUtc, utcToZonedTime, format } = require('date-fns-tz');

// Parse using location for offset
let loc = 'Canada/Saskatchewan';
let s   = '2020-08-14 13:05:52';
let fIn = 'yyyy-MM-dd HH:mm:ss';
let utcDate =  zonedTimeToUtc(s, loc);
// Show local equivalent
console.log(utcDate);

This leaves you somewhat at the mercy of the administrators of Saskatchewan, who might change the offset or introduce DST. An alternative is to append the exact offset you want to the timestamp and include it in the parse tokens:

// Parse using string for offset
let tz = '-06';
let utcDate2 =  parse(s + ' ' + tz,  fIn + ' X', new Date());
// Show local equivalent, should be same as above
console.log(utcDate2);

The advantage of the second method is that it doesn't require date-fns-tz and you aren't beholden to historic or future changes to Saskatchewan's offset (or that of any other IANA location).

Apparently there is a UTC module in development that will allow setting specific offsets like -6 rather than using IANA locations (can't find a link to that comment atm).

At this point the string has been parsed as GMT-6, but is still just a plain Date (i.e. just a time value with no idea of the timezone that was associated with the original string).

Once you have the date you can then show it as CST for output. To use an IANA location for the offset in call to format, you have to use format from date-fns-tz, not plain date-fns, otherwise it will just use the host system offset.

Note that the value in the format call is just setting the value to use for the offset string, it doesn't do anything to the actual date and time, that adjustment has already been applied by utcToZonedTime.

// Adjust to CST
let dCST = utcToZonedTime(utcDate2, loc);
// Format strings:
let fOut1 = 'yyyy-MM-dd HH:mm:ss XXX'; // -0600
let fOut2 = 'yyyy-MM-dd HH:mm:ss z';   // CST
// Format using location
console.log(format(dCST, fOut1, {timeZone: loc}));
console.log(format(dCST, fOut2, {timeZone: loc}));

I prefer the -0600 version as it avoids questions of whether DST is observed or not (and is really what the code is doing). Also, in the "z" version you might get the offset or the timezone name (probably depending on the host default language and location, which is a quirk of date-fns-tz using Intl.DateTimeFormat I think).

You can also manually add the timezone using a format string like:

let fOut = 'yyyy-MM-dd HH:mm:ss \'-0600\'';

which will produce an output like:

"2020-08-14 13:05:52 GMT-0600"

I don't think there is any way to set a specific offset like "-0600" for both parsing and formatting without including it in the call. I think moment.js and luxon allow it.

For completeness, here's some code you can run at npm.runkit.com since there's no CDN for the current date-fns version to allow the code to run here.

var {parse} = require('date-fns');
var {zonedTimeToUtc, utcToZonedTime, format } = require('date-fns-tz');

// Parse using location for offset
let loc = 'Canada/Saskatchewan';
let s   = '2020-08-14 13:05:52';
let fIn = 'yyyy-MM-dd HH:mm:ss';
let utcDate =  zonedTimeToUtc(s, loc);
// Show local equivalent
console.log(utcDate);

// Parse using string for offset
let tz = '-06';
let utcDate2 =  parse(s + ' ' + tz,  fIn + ' X', new Date());
// Show local equivalent, should be same as above
console.log(utcDate2);

// Format using location:
let fOut1 = 'yyyy-MM-dd HH:mm:ss XXX'; // -0600
let fOut2 = 'yyyy-MM-dd HH:mm:ss z';   // CST
let dCST = utcToZonedTime(utcDate2, loc);
console.log(format(dCST, fOut1, {timeZone: loc}));
console.log(format(dCST, fOut2, {timeZone: loc}));

这篇关于在JavaScript中将时间表示为CST-date-fns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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