How to format a date to look like 2006-04-16T19:45:42Z

P

pamela fluente

Hi,

I wish to create a string from the current date now() so that it looks
like
the format used by Excel, for instance:

2006-04-16T19:45:42Z

How would I achieve that?

Thank you,

-Pam

PS
Btw does that format has a name?
 
K

Kevin Spencer

I wish to create a string from the current date now() so that it looks
like
the format used by Excel, for instance:

2006-04-16T19:45:42Z

That is ISO 8601 Format. It indicates a UTC (Coordinated Unitversal Time)
time, which is the DateTime at 0 degrees longitude (Greenwhich Mean Time -
TMT). So, in order to format your DateTime value properly, you must first
convert it to UTC time, using the DateTime.ToUniversalTime() method, and
then call the DateTime.ToString(string format) overload, passing in the
following Custom DateTime string:

"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"

Unfortunately, the CLR includes Standard DateTime strings that come close,
but not exactly. The closest is "U" which translates to UTC time format
without the "T" before the hh:mm:ss portion.

Example:

DateTime.Now.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'")

Note that the time is translated to GMT time by default with the assumption
that it is local time. Otherwise, you need to convert the Time to local
first. You can read more about this subject here:

http://msdn2.microsoft.com/en-us/library/system.datetime.aspx

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
P

pamela fluente

That is ISO 8601 Format. It indicates a UTC (Coordinated Unitversal Time)
time, which is the DateTime at 0 degrees longitude (Greenwhich Mean Time -
TMT). So, in order to format your DateTime value properly, you must first
convert it to UTC time, using the DateTime.ToUniversalTime() method, and
then call the DateTime.ToString(string format) overload, passing in the
following Custom DateTime string:

"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"

Unfortunately, the CLR includes Standard DateTime strings that come close,
but not exactly. The closest is "U" which translates to UTC time format
without the "T" before the hh:mm:ss portion.

Thank you very much Kevin and Serge,

I will try that.

Very helpful

Cheers,

-Pam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top