Remove UtcOffset from DateTime.Now

I need a DateTime.Now value in round trip format without utcOffset. Based on this MSDN article, if you create a new DateTime instance without UtcOffset, it creates the format I want.

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0); // Displays 2008-04-10T06:30:00.0000000 

but if I use DateTime.Now, I get the offset in the line:

 DateTime.Now.ToString("o") // Displays 2012-02-08T14:11:12.8378703-05:00 

I could just use a substring or populate a DateTime instance without it, but I was wondering if there is a way to remove UtcOffset.

+4
source share
3 answers

A UTC offset (for example, β€œ-05: 00” or β€œZ”) is omitted if DateTime.Kind is neither Utc nor Local .

You can create such a DateTime value as follows:

 DateTime.UtcNow.ToString("o"); // "2012-02-08T19:19:38.5767158Z" new DateTime(DateTime.UtcNow.Ticks).ToString("o"); // "2012-02-08T19:19:38.5767158" new DateTime(DateTime.Now.Ticks).ToString("o"); // "2012-02-08T14:19:38.5767158" 
+5
source

If I understand what you want, you can use DateTime.UtcNow .

 DateTime.Now.ToString("o") DateTime.UtcNow.ToString("o") 

Outputs:

  2012-02-08T13: 18: 22.4459488-06: 00
 2012-02-08T19: 18: 22.4599488Z
+3
source

I will just reset using

 new DateTimeOffset(Date_Goes_Here, TimeSpan.Zero); 
0
source

Source: https://habr.com/ru/post/1395474/


All Articles