如何将DateTimeOffset ToString(“o”)偏移量设置为00

时间:2018-02-22 16:35:46

标签: c# c#-4.0 c#-3.0 c#-2.0

DateTimeOffset.Now = {2/22/2018 10:32:50 AM -06:00}已转换为 xmlformatTime = "2018-02-22T10:33:18.2670639-06:00",格式需要删除并预期 o / p 2018-02-22T10:33:18.00-06:00

18.2670639需要改为18.00

DateTimeOffset PassDate = DateTimeOffset.Now;
int minutes = PassDate.Minute;
int seconds = PassDate.Second;
var  offset = PassDate.Offset;
var xmlformatTime = PassDate.ToString("o");
Console.WriteLine(xmlformatTime)

enter image description here

尝试使用以下选项时。

var xmlformatTime = PassDate.ToString("'MM'-'dd'T'HH':'mm':'ss'.00'K");

enter image description here

1 个答案:

答案 0 :(得分:1)

来自MSDN

  

" O"或" o"标准格式说明符对应于DateTimeOffset值的"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz"自定义格式字符串

所以使用

var xmlformatTime = PassDate.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.00'zzz");

而是将毫秒更改为零。