user1502952
user1502952

Reputation: 1420

Date and time format conversion in C#

I have date/time format, for example:

"2012-06-28T08:26:57Z"

What kind of date/time format is that and how can it be converted into the following format, using DateTime format in C#.:

"8/24/2012 4:09:17 AM"

Upvotes: 1

Views: 10849

Answers (8)

seeker
seeker

Reputation: 3333

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx this is answer to your question. Here you can see how to create different date formats. According to this resource, you may use something like this:

String olddate="2012-06-28T08:26:57Z";
DateTime date=Convert.ToDateTime(olddate);
String date1=date.ToString("M/d/yyyy h:m:s tt");

Upvotes: 1

Vijay Rana
Vijay Rana

Reputation: 143

you can simply use :)

DateTime dt = Convert.ToDateTime("2012-06-28T08:26:57Z");

Upvotes: 0

Raghuveer
Raghuveer

Reputation: 2638

That is Universal Sortable date format

You can use following code to convert it

 var dt = DateTime.Parse("2012-06-28T08:26:57Z");
 var newdt = String.Format("{0:G}", dt);  // "6/28/2012 1:56:57 PM"

Update

You can try this also

var dt = DateTime.Parse("2012-06-28T08:26:57Z", System.Globalization.CultureInfo.InvariantCulture);
var newdt = String.Format("{0:G}", dt);

Upvotes: 1

Ilya Ivanov
Ilya Ivanov

Reputation: 23626

try to use something liKe this.

var d = DateTime.Parse("2012-08-24T04:09:17Z");
Console.WriteLine (d.ToString("G"), CultureInfo.InvariantCulture);

Note that 'General date/time pattern (long time).' in .net is culture specific. From msdn:

6/15/2009 1:45:30 PM -> 6/15/2009 1:45 PM (en-US)
6/15/2009 1:45:30 PM -> 15/06/2009 13:45 (es-ES)
6/15/2009 1:45:30 PM -> 2009/6/15 13:45 (zh-CN)

Upvotes: 1

ZafarYousafi
ZafarYousafi

Reputation: 10840

Try this to convert Universal datetime to local time

    var date = DateTime.ParseExact("2012-08-25T06:57:57Z","yyyy-MM-ddTHH:mm:ssZ",System.Globalization.CultureInfo.CurrentCulture);
var newformat = date.ToString("MM/dd/yyyy HH:mm:ss tt");

Upvotes: 1

Ivan Golović
Ivan Golović

Reputation: 8832

You can do this:

string input = "2012-06-28T08:26:57Z";
var dt = DateTime.Parse(input);
string output = dt.ToString(@"MM/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);

For the meaning of each part of your input string, take a look at this link: http://www.w3.org/TR/NOTE-datetime

Upvotes: 6

user149341
user149341

Reputation:

This is an ISO8601 date/time string. The numbers are the year, month, day, hour, minute, and second (in that order).

The "T" is a placeholder. It means nothing.

The "Z" is an indicator that the time is relative to GMT, rather than in a local time zone.

Upvotes: 2

Krishna Thota
Krishna Thota

Reputation: 7026

Try converting the date into string like this

date.ToString("yyyy-MM-dd HH':'mm':'ss")

Here date is a variable in which a date is present

or try this

string timeString = "11/12/2009 13:30:00.000";
IFormatProvider culture = new CultureInfo("en-US", true); 
DateTime dateVal = DateTime.ParseExact(timeString, "dd/MM/yyyy HH:mm:ss.fff", culture);

These links might also be helpful to you.

DateTime.ToString() Patterns

String Format for DateTime [C#]

Upvotes: 1

Related Questions