c#解析UTC datetime

编程入门 行业动态 更新时间:2024-10-11 23:27:13
本文介绍了c#解析UTC datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试解析11/23/2011 23:59:59 UTC +0800 as ac#datetime对象,但尝试使用标准的datetime解析方法,甚至datetime精确解析我得到无效的日期。

I am trying to parse 11/23/2011 23:59:59 UTC +0800 as a c# datetime object but trying the standard datetime parse method or even the datetime exact parse I get invalid date.

任何想法?

推荐答案

我建议您解析为 DateTimeOffset 而不是 DateTime ,时,MSDN中建议使用rel =noreferrer>

I would suggest you parse to a DateTimeOffset instead of a DateTime, as recommended in MSDN when using a time zone offset specifier in the format string:

using System; using System.Globalization; class Test { static void Main(string[] args) { string text = "11/23/2011 23:59:59 UTC +0800"; string pattern = "MM/dd/yyyy HH:mm:ss 'UTC' zzz"; DateTimeOffset dto = DateTimeOffset.ParseExact (text, pattern, CultureInfo.InvariantCulture); Console.WriteLine(dto); } }

然后,您可以将其转换为 DateTime 值,如果你想要的,但没有像$ DateTime 的偏移量为8小时 - 一个 DateTime 被视为通用的,本地的或未指定的,无法存储特定的偏移量。

You can then convert that to a DateTime value in UTC if you want, but there's no such thing as "a DateTime with an offset of 8 hours" - a DateTime is either regarded as universal, local or unspecified, with nowhere for a specific offset to be stored.

DateTime 是一个以各种方式好奇的类型,并且可能导致不必要的开发者出现问题。

DateTime is a curious type in various ways, and can cause problems for the unwary developer.

更多推荐

c#解析UTC datetime

本文发布于:2023-11-09 03:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UTC   datetime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!