Reputation: 21
I have a requirement to get time stamp with zone information as
2014-12-05T08:37:25+0300
To achieve this I used time format as
DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ssZ")
but it gave me the output as this format
2014-12-05T08:37:25Z
Can anybody have idea on how to get timezone information in this format
2014-12-05T08:37:25+0300
Upvotes: 2
Views: 88
Reputation: 98840
but it gave me the output as this format 2014-12-05T08:37:25Z
This is normal because Z
is not a standard or custom date and time format. That's why this character is copied to the result string unchanged.
I think your CurrentCulture
represents UTC Offset with +0300
format without TimeSeparator
, you can directly use The "K"
custom format specifier since DateTime.Now
returns Local
as a DateTimeKind
like;
DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ssK");
result probably will be;
2014-12-05T08:37:25+0300
Upvotes: 1
Reputation: 20764
You can use this
var now = DateTime.Now;
Console.WriteLine(now.ToString("yyyy-MM-ddTHH:mm:ss")
+ now.ToString("zzz").Replace(":", string.Empty));
removing ":" from zzz format string is not a built in function in .NET and easiest way to remove it is by replacing it.
Upvotes: 1