C#将日期时间对象转换为ISO 8601字符串 [英] c# convert datetime object to iso 8601 string

查看:618
本文介绍了C#将日期时间对象转换为ISO 8601字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将DateTime对象转换为ISO8601字符串,但始终得到错误的结果。我一直在研究stackoverflow,但是找不到正确的解决方案。

I'm trying to convert a DateTime object to a ISO8601 string but keep getting wrong results. I've looked around on stackoverflow, but couldn't find the right solution.

我从日期时间字符串 2017-06-26T20:45:00.070Z开始,由newtonsoft从json反序列化并转换为等效于C#的DateTime对象到:

I start with a date time string of "2017-06-26T20:45:00.070Z" which deserialized by newtonsoft from json and converted to a DateTime object in C# equivalent to :

var theTime = new DateTime(2017, 6, 26, 20, 45, 00, 70, DateTimeKind.Utc);

现在我需要将该时间转换回其原始UTC格式字符串,才能在其他算法中使用它,但是我尝试的每次转换都不会将其返回到原始字符串。不知道我在做什么错。

Now i need the convert that time back to it's original UTC format string to use it in another algorithm, but every conversion i try doesn't return it to that original string. Not sure what i'm doing wrong.

我尝试过:

var newTime = theTime.UtcNow.ToString("o");
// returns "2017-06-26T00:00:00.0000000Z"

var newTime2 = theTime.Date.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.sssZ");
// returns "2017-06-26T00:00:00.00Z"

什么是我做错了吗?我想要等效于js使用toISOString()的功能,这是我在newTime2日期时间格式中列出的内容,但也没有显示时间。

what am i doing wrong? I want the equivalent to what js will do using toISOString() which is what i have listed in the newTime2 date time format, but it's not showing times either.

谢谢!

推荐答案

观察:

// Your input
DateTime dt = new DateTime(2017, 6, 26, 20, 45, 0, 70, DateTimeKind.Utc);

// ISO8601 with 7 decimal places
string s1 = dt.ToString("o", CultureInfo.InvariantCulture);
//=> "2017-06-26T20:45:00.0700000Z"

// ISO8601 with 3 decimal places
string s2 = dt.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK", CultureInfo.InvariantCulture);
//=> "2017-06-26T20:45:00.070Z"

一些事情:


  • 不要在格式字符串中使用 Z 。这不是有效格式说明符,因此将其视为要输出的字符。无论输入日期时间 .kind 设置如何,它都会出现在每个字符串中。

  • Don't use Z in the format string. That's not a valid format specifier, so it is treated as just a character to output. It will be in every string, regardless of .Kind setting of the input datetime.

With DateTime ,使用 K -正确传达 .Kind Z 附加到 DateTimeKind.Utc 的输出中,或者将 DateTimeKind的UTC偏移本地,或者对于 DateTimeKind完全不显示。未指定

With DateTime, use K - which properly conveys the .Kind by appending a Z to the output for DateTimeKind.Utc, or an offset from UTC for DateTimeKind.Local, or nothing at all for DateTimeKind.Unspecified.

尽管 T 将作为字符输出,因为它不是有效的格式说明符,但我建议始终对这些内容保持明确,因此建议使用'T'

Though T will output as a character because it's not a valid format specifier, I suggest always being explicit about those things, so prefer 'T'.

使用 fff 总是会返回三个小数(毫秒)。如果希望零时省略小数,请改用 FFF 。您对 sss 的使用无效。

Using fff will always give you back three decimals (milliseconds). If you want the decimals omitted when zero, use FFF instead. Your use of sss is not valid.

传递 CultureInfo.InvariantCulture 是一个好习惯,因为它可以帮助您避免在当前区域性可能使用其他日历系统的情况下出现的问题。例如, ar-SA 使用 UmAlQuraCalendar ,而不是ISO 8601要求的公历。

Passing CultureInfo.InvariantCulture is a good practice, as it helps you avoid problems where the current culture might use a different calendar system. For example ar-SA uses the UmAlQuraCalendar, rather than the proleptic Gregorian calendar required by ISO 8601.

在您尝试的代码中,您调用了 theTime.UtcNow -无法编译。 UtcNow 是静态属性,而不是实例属性。

In your code you tried, you had called theTime.UtcNow - that won't compile. UtcNow is a static property, not an instance property.

在您的代码中也称为 theTime.Date.ToUniveralTime()-也不要这样做。 .Date 将时间分量设置为零,而 .ToUniversalTime()将无效,因为输入值已经具有 DateTimeKind.Utc

Also in your code you called theTime.Date.ToUniveralTime() - Don't do that either. .Date will set the time components to zero, and .ToUniversalTime() will have no effect since the input value already has DateTimeKind.Utc.

这篇关于C#将日期时间对象转换为ISO 8601字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆