JavaScript的ASP.net日期格式没有时区信息 - 时区偏移 [英] Javascript ASP.net date format without timezone info - timezone offsets

查看:234
本文介绍了JavaScript的ASP.net日期格式没有时区信息 - 时区偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端的JavaScript生成一个JavaScript日期(新的Date(2007,5,1))。

I have a client side JavaScript that generates a date in JavaScript( new Date(2007,5,1)).

我需要在此日期穿过一个隐藏字段的code背后都可以访问。

I need this date passed through to a hidden field that the code behind can access.

我的问题是,当隐藏字段被转换成一DOTNET的日期时间,时间是不正确。这是因为,JavaScript是包括来自客户端浏览器时区信息。

My issue is that when the hidden field is converted into a DotNet datetime, the time is incorrect. This is because the JavaScript is including timezone info from the client browser.

DOTNET的然后使用这个信息的基础上的服务器时间和客户端的时间之间的差来重新计算的时间。

DotNet is then using this info to recalculate the time based on the difference between the server time and the client time.

我从JavaScript的只需要年,月,日是。

What i need from the JavaScript is just the year, month and day.

我不希望通过3 INT值传递给后面的,因为这将是整个应用程序的重大改变我的code。

I don't want to pass through 3 int values to my code behind as this will be a major change to the whole app.

什么是对我来说,做到这一点的最好方法是什么?

What is the best way for me to accomplish this?

如果我可以设置UTC时间没有时区的信息,我认为这可能会奏效。

If i can set a UTC time with no timezone info I think that might work.

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

您可以使用DateTimeOffset.ParseExact使用您指定的格式解析字符串到的DateTimeOffset值:

You can use DateTimeOffset.ParseExact to parse a string to a DateTimeOffset value using the format you specify:

        string dateString = "Fri Jun 01 2007 00:00:00 GMT+08:00";
        DateTimeOffset date = DateTimeOffset.ParseExact(dateString, "ddd MMM dd yyyy hh:mm:ss 'GMT'zzz", CultureInfo.InvariantCulture);

您必须把GMT在引号,否则M将PTED为格式字符间$ P $。

You have to put GMT in quotes otherwise M will be interpreted as a format character.

可惜的是,它是不能忽视的字符串值的一部分。如果字符串包含时区的名称,你必须先拆并没有说明得到这个角色。

Unfortunatelly, it is not possible to ignore part of the string value. If your string includes the name of the timezone you have to split it first and get the part without the description

        string dateString = "Fri Jun 01 2007 00:00:00 GMT+08:00 (Taipei Standard Time)";
        var parts=dateString.Split('(');
        string datePart = parts[0].TrimEnd();
        var date=DateTimeOffset.ParseExact(datePart,"ddd MMM dd yyyy hh:mm:ss 'GMT'zzz",CultureInfo.InvariantCulture);

这篇关于JavaScript的ASP.net日期格式没有时区信息 - 时区偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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