代理代码从XML读取DATETIME("2016-05-08T12:33:11.991-05:00"),然后创建DATETIME对象。然后我的代码读取DateTime。我想获得XML文件中的时间,但没有TZ("2016-05-08 12:33:11.991000")。我怎么得到它?
(注意,我不能访问用于从XML文件创建DateTime对象的代码。DateTime。)
psuedocode
Input-XML "2016-05-08T12:33:11.991-05:00". //Creates DateTime object called DtXML.
.....
I do not have access to this code.
.....
DtXML.ToString("yyyy-MM-dd HH:mm:ss.ffffff"); //This gives 2016-05-08 10:33:11.991000
DtXML.Kind;// This is LOCAL.
I want to get "2016-05-08 12:33:11.991000".
Is this possible?
您可以使用DateTimeOffset
来完成此操作。
string s = "2016-05-08T12:33:11.991-05:00";
var dtOffset =DateTimeOffset.Parse(s, null);
DateTime dt = dtOffset.DateTime; // 08.05.2016 12:33:11
Console.WriteLine(dtOffset.DateTime.ToString("yyyy-MM-dd HH:mm:ss.ffffff")); // prints - 2016-05-08 12:33:11.991000
Check this code
你试过吗?
var expectedDT = DateTime.SpecifyKind(DtXML.ToUniversalTime(), DateTimeKind.Local);