fre*_*k91 3 c# datetime windows-rt windows-phone-8.1
将DateTime转换为此格式的最佳和最快方法是什么?
2015-03-26T18:02:58.145798Z
目前我从服务器收到一个日期,我能够解析它并将日期转换为DateTime,ToString()输出是这样的:
26/03/2015 18:02:58
为了转换我正在使用这行代码的日期:
var parsedDate = DateTime.Parse("2015-03-26T18:02:58.145798Z", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
Run Code Online (Sandbox Code Playgroud)
将parsedDate转换回原始格式的最佳方法是什么?
编辑:我想将DateTime转换为此格式2015-03-26T18:02:58.145798Z为字符串
如果您有一个DateTime对象,可以使用Oas格式说明符将其转换为具有该特定格式的字符串:
parsedDate.ToString("O")
Run Code Online (Sandbox Code Playgroud)
要么
parsedDate.ToUniversalTime().ToString("O") // if parsedDate is not UTC
Run Code Online (Sandbox Code Playgroud)
回报"2015-03-26T18:02:58.1457980Z".
如果DateTimeKind您的DateTime对象不是,Utc则Z根据ISO8601,您将无法在字符串末尾获得扩展名.在您提供的示例Z中,因为DateTime.Parse将识别它并返回DateTimein Utc.如果Z您解析的原始字符串中缺少,您仍然可以通过使用ToUniversalTime()日期时间对象来假设它是UTC .