I want DateTime
in the following format.
DateTime a = Convert.ToDateTime(DateTime.UtcNow.ToString("s") + "Z");
Output:
2018-05-29T09:16:59Z
I want to subtract
4 hours from this time. So I have used this line of code:
var result = a.AddHours(-4);
Now, not only is the wrong time being shown, the above format is also disturbed.
29-05-2018 10:52:51
Expected Output:
2018-05-29T05:16:59Z
To get UTCNow minus 4 hours you'd want to do:
var fourHoursAgo = DateTime.UtcNow.AddHours(-4);
Console.WriteLine("fourHoursAgo: " + fourHoursAgo.ToString("yyyy-MM-ddTHH:mm:ssK"));
This will give an output like:
2018-05-29T05:36:18Z
This is basically an ISO 8601 format, very similar to DateTime.ToString("s"), but including the timezone ("Z" in this case.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With