简体   繁体   English

使用 moment.js 更改日期时间戳上的时区,而不更改时间

[英]Use moment.js to change the timezone on a datetime stamp, without changing the time

I am migrating event data from an old SQL database to a new Mongo database, using NodeJS.我正在使用 NodeJS 将事件数据从旧的 SQL 数据库迁移到新的 Mongo 数据库。 However, whoever set up the SQL database created all of the dates for the events, made the times in PST/PDT, but the database believes they are in UTC time.但是,设置 SQL 数据库的人创建了事件的所有日期,在 PST/PDT 中创建了时间,但数据库认为它们是 UTC 时间。

For Example: A date from the SQL database may be: 23-APR-10 which MomentJS shows as: 2010-04-23T21:00:00Z when 21:00:00 is the PST time.例如:SQL 数据库中的日期可能是: 23-APR-10 ,其中 MomentJS 显示为: 2010-04-23T21:00:00Z21:00:00是 PST 时间时。

Is it possible to use pure JavaScript/MomentJS/NodeJS or a different npm module to change the timezone on the DateTime string without modifying the time (ie 2010-04-23T21:00:00Z would become 2010-04-23T21:00:00-8:00 )?是否可以使用纯 JavaScript/MomentJS/NodeJS 或不同的 npm 模块来更改 DateTime 字符串上的时区而不修改时间(即2010-04-23T21:00:00Z将变为2010-04-23T21:00:00-8:00 )?

PS.附注。 Even though the SQL database only shows DD-MMM-YY but returns a DateTime string when I query it.即使 SQL 数据库只显示 DD-MMM-YY 但在我查询时返回一个 DateTime 字符串。

Following the line of inquiry in the question comments, it seems your problem is that due to the timezone mishap, the timestamps stored in the db are stored without the timezone offset, and since your desired timezone is PST (UTC-8hours), the timestamps are 8 hours ahead, for instance, what should have been 2010-04-23T13:00:00Z has become 2010-04-23T21:00:00Z .按照问题评论中的查询行,您的问题似乎是由于时区事故,存储在数据库中的时间戳没有时区偏移,并且由于您想要的时区是 PST(UTC-8 小时),时间戳例如,提前 8 小时,本来应该是2010-04-23T13:00:00Z已经变成了2010-04-23T21:00:00Z

So what needs to be done here is that the utc offset for your desired timezone needs to be obtained and added to the date.所以这里需要做的是需要获取您所需时区的 utc 偏移量并将其添加到日期中。

The offset in your case is known (-8 hours).您的情况下的偏移量是已知的(-8 小时)。 However, we can fetch the correct offset of any desired timezone from the moment-timezone library.但是,我们可以从moment-timezone库中获取任何所需时区的正确偏移量。

const moment_timezone = require('moment-timezone');

//a sample timestamp you're getting from your db
const myDateObj = new Date("2010-04-23T21:00:00Z");

//timezone for PST as understood by moment-timezone
const myMomentTimezone = "America/Los_Angeles";

//offset for your timezone in milliseconds
const myTimezoneOffset = moment_timezone.tz(myMomentTimezone).utcOffset()*60000;

//perfom the correction
const getCorrectedDateObj = (givenDateObj) => new Date(givenDateObj.valueOf() + myTimezoneOffset);

console.log(getCorrectedDateObj(myDateObj));

You may notice that we are actually changing the timestamp, because the given timestamp and the requried timestamp are, due to the nature of the error, essentially different timestamps.您可能会注意到我们实际上正在更改时间戳,因为由于错误的性质,给定的时间戳和所需的时间戳本质上是不同的时间戳。 Moment-timezone is only being used here to fetch the offset, it's not used to "convert" anything. Moment-timezone 仅用于此处获取偏移量,不用于“转换”任何内容。

Anuj Pancholi's answer is correct, however;然而,Anuj Pancholi 的回答是正确的; the old SQL database I'm using seems to have a lot of quirks, so I had to modify his answer to get my code working.我正在使用的旧 SQL 数据库似乎有很多怪癖,所以我不得不修改他的答案才能让我的代码正常工作。

My Solution:我的解决方案:

function getCorrectedDateObj(myDateObj){
  const timezoneOffset = momentTimeZone.tz(timezone).utcOffset() * 60000;
  const dt = new Date(myDateObj.valueOf() + timezoneOffset / 2);
  dt.setHours(dt.getHours() + 12);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM