Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert time in c#

Tags:

c#

datetime

How do i convert this date 1/6/2016 12:00:00 AM to 2016-01-06T00:00:00 in C#. I need to display it like that.

EstimatedDate.ToString("");

SOLVE:

string.Format("{0:s}", EstimatedDate)
like image 384
Reynan Avatar asked Dec 10 '22 18:12

Reynan


2 Answers

You can use The "s" standard format specifier which is exactly generate the result you want.

EstimatedDate.ToString("s");

From documentation;

The pattern reflects a defined standard (ISO 8601), and the property is read-only. Therefore, it is always the same, regardless of the culture used or the format provider supplied. The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss".

like image 181
Soner Gönül Avatar answered Jan 03 '23 21:01

Soner Gönül


EstimatedDate.ToString("yyyy-MM-ddTHH:mm:ss"); sill give you the desired format

For a full example of:

DateTime date = DateTime.Parse("1/6/2016 12:00:00 AM");
string dateStr = date.ToString("yyyy-MM-ddTHH:mm:ss");

Update from @soner-gönül: - This method is Cultural dependent, where as "s" is not. CurrentCulture must be Gregorian Calendar for this to work.

cs timestamp time

like image 44
iSkore Avatar answered Jan 03 '23 22:01

iSkore