[英]How to convert C# datetime value to UTC timezone
I am in need to pass value of type DateTime to an API in the below format 我需要以以下格式将DateTime类型的值传递给API
2018-12-02T07:00:00.000Z //required format
but the datetime value from Db I am getting is as 但是我从Db得到的datetime值是
2018-10-25 05:30:00.0000000
Now using the .ToString("o")
, the value formatted as 现在使用.ToString("o")
,将值格式化为
2018-10-25T05:30:00.0000000
Checked below SO links 在下面检查SO链接
Where's the DateTime 'Z' format specifier? DateTime'Z'格式说明符在哪里?
http://stackoverflow.com/questions/9163977/converting-datetime-to-z-format http://stackoverflow.com/questions/9163977/converting-datetime-to-z-format
but not converting into the required format. 但不会转换为所需的格式。
My understanding is having Z in the datetime represents UtcTimeZone. 我的理解是在日期时间中使用Z表示UtcTimeZone。
Are you looking for something like this? 您是否正在寻找这样的东西?
var baseTime = DateTime.Parse("2018-10-25 05:30:00.0000000");
var utcTime = baseTime.ToUniversalTime();
Console.WriteLine(utcTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"));
You can try it out here: https://dotnetfiddle.net/HsdMcu 您可以在这里尝试: https : //dotnetfiddle.net/HsdMcu
If you have summer and winter time in your region you might need to use a bit more sophisticated way for parsing your intial time correctly, this can be found in this answer . 如果您所在地区有夏季和冬季,则可能需要使用更复杂的方法来正确解析初始时间,可以在此答案中找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.