Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format datetime timezone

Tags:

c#

datetime

I have a DateTime object. I want to return a String formatted as this one below:

Thu, 06 June 2010 16:00:00 +0200

This is my code so far:

DateTime.Now.ToString("ddd, dd MMMM yyyy HH:mm:ss zzz");

Result:

Thu, 10 Juni 2010 18:33:14 +02:00

Is there a built-in way to get the timezone difference formatted without : ? (without manually stripping the :. Don't know if there are any complications, if I do so)

like image 231
citronas Avatar asked Jun 10 '10 16:06

citronas


2 Answers

Note you can use "o" to have a full datetime with time zone. It can be usefull for log stuff.

DateTime.Now.ToString("o") => 2012-08-17T17:31:19.7538909+08:00
like image 199
Olivier de Rivoyre Avatar answered Oct 17 '22 15:10

Olivier de Rivoyre


It seems (I've searched), there's no way apart manipulating the resulting string. If you use zz you get only "+02" however... then you could append 00, instead of "searching"(regex or whatever) for the last : and strip it. DateTimeFormatInfo allows to know the separator for h:m:s, d/m/y, but not timezone; moreover if DateTimeFormatInfo.TimeSeparator affects timezone too (being h:m), you can't search for : snce it could not work on all locales, you should search for DateTimeFormatInfo.TimeSeparator instead; or zz and append 00 at the end... For now, it is all about my ideas to help you.

like image 2
ShinTakezou Avatar answered Oct 17 '22 14:10

ShinTakezou