Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get DateTime.Now() in YYYY-MM-DDThh:mm:ssTZD format using C# [duplicate]

Tags:

c#

datetime

Possible Duplicate:
datetime to string with time zone

This is one of the W3C standard date time format that I want to use in sitemap. This DateTime standard is:

Complete date plus hours, minutes and seconds: YYYY-MM-DDThh:mm:ssTZD where TZD = time zone designator (Z or +hh:mm or -hh:mm) (eg 1997-07-16T19:20:30+01:00)

I am using the following code to get the current DateTime in that format:

DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssTZD"); 

But this gives: 2011-08-10T02:27:20TZD

Apparently the DateTime.Now doesn't recognize "TZD" in the parameter. Please help. How can I get the current DateTime in this format?

like image 907
Umair Khan Jadoon Avatar asked Aug 09 '11 21:08

Umair Khan Jadoon


People also ask

What is SSSZ in date format?

Dates are formatted using the following format: "yyyy-MM-dd'T'hh:mm:ss'Z'" if in UTC or "yyyy-MM-dd'T'hh:mm:ss[+|-]hh:mm" otherwise. On the contrary to the time zone, by default the number of milliseconds is not displayed. However, when displayed, the format is: "yyyy-MM-dd'T'hh:mm:ss.

What is datetime format TZ?

The DATETIME-TZ data type consists of three parts: · An ABL date and time (as for DATETIME) and. · An integer representing the time zone offset from Coordinated Universal Time (UTC) in minutes. ABL stores DATETIME-TZ data in UTC, along with the time zone offset of the DATETIME-TZ.

What is datetime now in C#?

Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time. public: static property DateTime Now { DateTime get(); }; C# Copy.


2 Answers

Use the zzz format specifier to get the timezone offset as hours and minutes. You also want to use the HH format specifier to get the hours in 24 hour format.

DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz") 

Result:

2011-08-09T23:49:58+02:00 

Some culture settings uses periods instead of colons for time, so you might want to use literal colons instead of time separators:

DateTime.Now.ToString("yyyy-MM-ddTHH':'mm':'sszzz") 

Custom Date and Time Format Strings

like image 102
Guffa Avatar answered Sep 29 '22 00:09

Guffa


use zzz instead of TZD

Example:

DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz"); 

Response:

2011-08-09T11:50:00:02+02:00 
like image 43
genesis Avatar answered Sep 29 '22 01:09

genesis