使用 C# 将 GMT 日期转换为本地时间时遇到问题?

发布于 2024-08-22 15:07:02 字数 283 浏览 1 评论 0原文

我们有一个用 C#(紧凑框架)编写的 Windows Mobile 应用程序。区域设置设置为(英语)新西兰。新西兰时区设置为 GMT+12。

我们以 GMT/UTC 格式存储日期。 我们的日期是 UTC 时间 2010-02-18 18:00:00,

新西兰的时间是上午 7:00。

当我们调用日期时间对象时,

starttime = starttime.ToLocalTime();

我们得到上午 9:00。
我们做错了什么?

We have a Windows Mobile application written in C# (compact framework). Regional setting is set to (English) New Zealand. Time zone is set to GMT+12 New Zealand.

We store our dates in GMT/UTC format.
We have a date 2010-02-18 18:00:00 in UTC

This time in New Zealand is 7:00 am.

When we call on a datetime object

starttime = starttime.ToLocalTime();

we get 9:00 am.
What are we doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一刻暧昧 2024-08-29 15:07:02

您是否指定了该日期时间的“种类”?像这样的事情:

DateTime parsedStartTime = DateTime.SpecifyKind(
    DateTime.Parse(starttime),
    DateTimeKind.Utc);

DateTime localStartTime = parsedStartTime.DateToLocalTime();

这可能会有所帮助,因为它可能不知道您现在拥有的日期时间是 Utc 类型(可能未指定)。

如果这没有帮助,也许您的一些显示如何设置开始时间的代码会有所帮助。

Have you specified the "kind" on that datetime? Something like this:

DateTime parsedStartTime = DateTime.SpecifyKind(
    DateTime.Parse(starttime),
    DateTimeKind.Utc);

DateTime localStartTime = parsedStartTime.DateToLocalTime();

That might help as it might not know that the datetime you have now is in the type of Utc (it is probably unspecified).

If that doesn't help maybe some of your code showing how you are setting starttime would help.

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