Mano
Mano

Reputation: 73

Converting a Datetime from the datatable to a specific format of string C# ASP.NET

I am wokring on C#, ASP.Net So I take a datetime from the database and turn it into a datatable column. As for me, the database's datetime is very long and includes seconds and all sort of stuff and I want to change it into a specific format of:

dd/MM/yy hh:mm

so Iv'e tried this:

lblDate.Text=(DateTime.ParseExact(dt.Rows[0]["PMDate"].ToString(),"dd/MM/yy hh:mm",System.Globalization.CultureInfo.InvariantCulture)).ToString();

But sadly I get an error saying that the string was not identified as a valid DateTime. sadly, it is referring to the whole line above so I cannot tell what I did wrong.

All I want to do is to take the DateTime from the DataBase, turn it into a DataTable column and from there into a string in the format mentioned above.

Please help me, thanks in advance.

Upvotes: 2

Views: 15595

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241475

This is probably what you should do:

DateTime pmdate = (DateTime) dt.Rows[0]["PMDate"];
lblDate.Text = pmdate.ToString("g");

Note, this will render the string using the general date/time pattern for the current culture.

Upvotes: 2

Related Questions