.NET日期,Moment.js,UTC和时区转换 [英] .NET date, Moment.js, UTC and Timezone shifting

查看:51
本文介绍了.NET日期,Moment.js,UTC和时区转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Ajax调用获得了 UTC 日期,例如"/日期(1517216466000 + 0100)/"
打印到控制台时的时间: 2018年1月29日星期一10:01:06 GMT + 0100(欧洲标准时间).

我需要做的是让用户更改时区:例如, moment(myDate).tz("Japan").

然后,我需要将日期保存为UTC格式,而我无法执行.
我一直在尝试 moment.utc(),但是对于上面的输入,它返回的时间比少1小时.

如何处理这种情况?总结:
1.从Web服务获取UTC时间
2.让用户更改时区
3.将修改后的日期保存在UTC中(不带时区)
工作演示: https://stackblitz.com/edit/angular-kqrct7?file = app%2Fapp.component.html

编辑以进行澄清:
让我们看一下时间.我从WCF得到的日期是10点.浏览器将其解释为GMT + 1中的10点,但是当我将其转换为UTC时,它变为9点.
我希望现在是 UTC时间.然后,如果我修改了时区,例如该日期的分钟数,我希望能够获得该日期的UTC值.

简化了我的问题

I've got a UTC date through an Ajax call, e.g. "/Date(1517216466000+0100)/",
which is when printing to the console: Mon Jan 29 2018 10:01:06 GMT+0100 (W. Europe Standard Time).

What I need to do, is to let the user change the timezones: I can easily do it with e.g. moment(myDate).tz("Japan").

Then I need to save the date in it's UTC format, which I am not able to do.
I've been experimenting with moment.utc(), but for the input above, it returns 1 hour less.

How to deal with this situation? In summary:
1. Get a UTC time from a webservice
2. Let the user change the timezones
3. Save the modified date in UTC (without the timezone)
Working demo: https://stackblitz.com/edit/angular-kqrct7?file=app%2Fapp.component.html

EDIT for clarification:
Let's just have a look at the hours. What I get the date from the WCF, is 10 o'clock. The browser interprets it as 10 o'clock BUT in GMT+1 so when I convert it to UTC, it becomes 9 o'clock.
I want it to be 10 o'clock as UTC. Then, if I modify the timezone and e.g. the minutes of this date, I want to be able to get the UTC value of this date.

Made my question simplier for clarification

  1. 我有一个UTC日期,该日期是从Web服务获得的,例如:"/Date(1517216466000 + 0100)/",它是:2018年1月29日星期一10:01:06 GMT + 0100(W.欧洲标准时间)打印到控制台时.

  1. I've got a UTC date, which I get from a webservice like: "/Date(1517216466000+0100)/" which is: Mon Jan 29 2018 10:01:06 GMT+0100 (W. Europe Standard Time) when printed to console.

我在其中添加带有time(this.inputDate).tz("Europe/Berlin").format()的时区,但由于我的浏览器是格林尼治标准时间+,所以它保持10:01:061.

I add a timezone to it with moment(this.inputDate).tz("Europe/Berlin").format(), but it stays 10:01:06, I guess because of my browsers GMT+1.

我希望将原始字符串用作UTC日期,并且应保留10:01:06,而不是09:01:06(如您在上面看到的(第二时刻示例)),因此时区为欧洲/柏林"将是11:01:6

I want the ORIGINAL string to be used as a UTC date AND it should remain 10:01:06, not 09:01:06 as you can see above (2nd moment example), so with the timezone "Europe/Berlin" would be 11:01:6

推荐答案

感谢大家的详细回答,它们对我的理解很有帮助!但是,正确的解决方案如下:

10点是数据库中的UTC时间,在Moment.js中它被解释为UTC 9点,因为C#将其作为本地时间处理.因此,在将日期发送给客户之前,我必须指出其UTC:

Thanks everyone for your detailed answers, they helped me a lot in understanding my problem! However, the right solution was the following:

10 o'clock was a UTC time in the database and it got interpreted as 9 o'clock UTC in Moment.js because C# handled it as a local time. So, before sending the date to the client, I had to indicate that its a UTC:

var utcToClient = DateTime.SpecifyKind(downtime.DownTimeStartUTC, DateTimeKind.Utc)

然后,我马上就可以创建一个带有以下内容的UTC:

Then, in Moment I could create a UTC with:

var jsUtc = moment.utc(downtime.DownTimeStartUTC)

更改时区变得轻而易举:

Changing the timezones was a breeze with:

jsUtc.tz(userSelectedTimezone)

并将日期保存在数据库中,我在C#中使用了它:

And saving the date in the database, I used this in C#:

var utcFromClient = Record.DownTimeStartUTC.ToUniversalTime()

这篇关于.NET日期,Moment.js,UTC和时区转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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