[英]C# Customize DateTime offset format in ToString()
Is it possible to produce an output like 2021-04-14 01:23:45.123456+0800
using DateTime.ToString()
?是否可以使用DateTime.ToString()
生成像2021-04-14 01:23:45.123456+0800
这样的 output ?
I've tried to use .ToString("yyyy-MM-dd hh:mm:ss.ffffff")
followed by some combination of z
, zz
, zzz
, etc, but cannot seem to get the offset portion of the string to not contain the :
.我尝试使用.ToString("yyyy-MM-dd hh:mm:ss.ffffff")
后跟z
、 zz
、 zzz
等的某种组合,但似乎无法让字符串的偏移部分不包含:
。
ie it produces 2021-04-14 01:23:45.123456+08:00
, but I need the +08:00
to be +0800
.即它产生2021-04-14 01:23:45.123456+08:00
,但我需要+08:00
为+0800
。
There's not a custom timezone format that will give you the full timezone without the colon.没有自定义时区格式可以为您提供不带冒号的完整时区。 The simplest solution (IMHO) is to just remove the colon and add the timezone manually:最简单的解决方案(恕我直言)是删除冒号并手动添加时区:
dt.ToString("yyyy-MM-dd hh:mm:ss.ffffff") +
dt.ToString("zzz").Replace(":","");
Or you could add the time zone to the string format and remove the "last" colon:或者您可以将时区添加到字符串格式并删除“最后一个”冒号:
s = dt.ToString("yyyy-MM-dd hh:mm:ss.ffffffzzz");
s = s.Remove(s.LastIndexOf(':'),1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.