Reputation: 19
How to get date time in
2017-06-15T14:20:30+02:00
format from
DateTime.utc.now
It is an ISO 8601 date format. I tried with
DateTime.UtcNow.ToString(@"yyyy-MM-ddTHH\:mm\:ss") + DateTimeOffset.UtcNow.ToString(@"zzz");
But the date I am getting is
2017-10-20-T09:29:20+00:00
The application is hosted in Azure.
Upvotes: 1
Views: 213
Reputation: 2415
I think you are misunderstanding ISO8601. For UtcNow
, the offset suffix will always be +00:00. The format shows local time, and the suffix means how much the local time is offset relatively to UTC time.
UtcNow
is the current time in the UTC zone, not the local zone - ergo, its offset to UTC is 0.
Upvotes: 1
Reputation: 106
try this one.
DateTime.UtcNow.ToString(@"yyyy-MM-ddTHH\:mm\:sszzz")
Upvotes: 0
Reputation: 222
DateTime.UtcNow.ToString(@"yyyy-MM-ddTHH:mm:ss") + DateTimeOffset.Now.Offset.ToString();
This is how this works:
EX 1:
DateTimeOffset localTime = DateTimeOffset.Now;
DateTimeOffset utcTime = DateTimeOffset.UtcNow;
Console.WriteLine("Local Time: {0}", localTime.ToString("T"));
Console.WriteLine("Difference from UTC: {0}", localTime.Offset.ToString());
Console.WriteLine("UTC: {0}", utcTime.ToString("T"));
// If run on a particular date at 1:19 PM, the example produces
// the following output:
// Local Time: 1:19:43 PM
// Difference from UTC: -07:00:00
// UTC: 8:19:43 PM
Ex 2:
var dt = new DateTime(2010, 1, 1, 1, 1, 1, DateTimeKind.Utc);
string s = dt.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss \"GMT\"zzz");
Console.WriteLine(s);
Upvotes: 0