没有时区信息的 Javascript ASP.net 日期格式 - 时区偏移

发布于 2024-09-08 11:19:08 字数 411 浏览 10 评论 0原文

我有一个客户端 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

屋顶上的小猫咪 2024-09-15 11:19:08

演示

如果我理解正确,

您需要 .toDateString()

var date = new Date(2007,5,1);

document.write(date);
document.write("<br><br>versus<br><br>");
document.write(date.toDateString());

打印

Fri Jun 01 2007 00:00:00 GMT+0800 (Taipei Standard Time)

versus

Fri Jun 01 2007

demo

If I understood it correctly,

you need .toDateString()

var date = new Date(2007,5,1);

document.write(date);
document.write("<br><br>versus<br><br>");
document.write(date.toDateString());

prints

Fri Jun 01 2007 00:00:00 GMT+0800 (Taipei Standard Time)

versus

Fri Jun 01 2007
月棠 2024-09-15 11:19:08

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

        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 将被解释为格式字符。

不幸的是,不可能忽略部分字符串值。如果您的字符串包含时区名称,则必须先将其拆分并获取没有描述的部分

        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);

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);

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);
囍笑 2024-09-15 11:19:08

您可以从 javascript Date 对象构建一个字符串您已经创建了 - 它具有 getDate()getMonth()getFullYear() 方法,您可以使用它们来构建您想要的确切字符串想要在隐藏字段中。

You can build up a string from the javascript Date object you have created - it has getDate(), getMonth() and getFullYear() methods that you can use to build up the exact string you want in the hidden field.

暮年 2024-09-15 11:19:08

当您获取代码隐藏文件中的值时,我建议使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文