简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM