没有时区信息的 Javascript ASP.net 日期格式 - 时区偏移
我有一个客户端 JavaScript,可以在 JavaScript 中生成日期(new Date(2007,5,1)
)。
我需要将此日期传递到后面的代码可以访问的隐藏字段。
我的问题是,当隐藏字段转换为 DotNet 日期时间时,时间不正确。这是因为 JavaScript 包含来自客户端浏览器的时区信息。
然后,DotNet 使用此信息根据服务器时间和客户端时间之间的差异重新计算时间。
我从 JavaScript 中需要的只是年、月和日。
我不想将 3 个 int 值传递给后面的代码,因为这将是对整个应用程序的重大更改。
对我来说实现这一目标的最佳方法是什么?
如果我可以设置没有时区信息的 UTC 时间,我认为这可能会起作用。
任何帮助表示赞赏。
I have a client side JavaScript that generates a date in JavaScript( new Date(2007,5,1)
).
I need this date passed through to a hidden field that the code behind can access.
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 is then using this info to recalculate the time based on the difference between the server time and the client time.
What i need from the JavaScript is just the year, month and day.
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?
If i can set a UTC time with no timezone info I think that might work.
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
演示
如果我理解正确,
您需要
.toDateString()
打印
demo
If I understood it correctly,
you need
.toDateString()
prints
您可以使用 DateTimeOffset.ParseExact 使用您指定的格式将字符串解析为 DateTimeOffset 值:
您必须将 GMT 放在引号中,否则 M 将被解释为格式字符。
不幸的是,不可能忽略部分字符串值。如果您的字符串包含时区名称,则必须先将其拆分并获取没有描述的部分
You can use DateTimeOffset.ParseExact to parse a string to a DateTimeOffset value using the format you specify:
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
您可以从 javascript
Date
对象构建一个字符串您已经创建了 - 它具有getDate()
、getMonth()
和getFullYear()
方法,您可以使用它们来构建您想要的确切字符串想要在隐藏字段中。You can build up a string from the javascript
Date
object you have created - it hasgetDate()
,getMonth()
andgetFullYear()
methods that you can use to build up the exact string you want in the hidden field.当您获取代码隐藏文件中的值时,我建议使用 C# 中的格式规范。让我解释一下我的意思 -
JavaScript 中 Date(...) 的日期时间格式如下
“Tue Jun 1 11:12:15 UTC+0530 2010”,
在 C# 中将转换为以下格式字符串 -
具有此格式字符串的“
ddd MMM d hh:mm:ss UTCzzz yyyy
”使用
DateTime.ParseExact(string, format,provider)
来获取C# 中日期时间的正确值。使用提供程序作为
System.Globalization.CultureInfo.InvariantCulture
。I would recommend to use a format specification in C# when you get the values in the code behind file. Let me explain what I mean -
The date time format for the Date(...) in JavaScript is as follows
"Tue Jun 1 11:12:15 UTC+0530 2010"
which in C# would translate to the following format string -
"
ddd MMM d hh:mm:ss UTCzzz yyyy
"with this format string use the
DateTime.ParseExact(string <Hidden Field Value>, format, provider)
to get the correct value for the datetime in C#.Use provider as
System.Globalization.CultureInfo.InvariantCulture
.