Formatting DateTime in C# -


i trying output datetime in 12 hour format. works , breaks.

code:

while (myreader.read()) {     console.writeline("date before formatting = " + myreader["date"].tostring());     datetime dt = datetime.parseexact(myreader["date"].tostring(), "yyyy/mm/dd hh:mm:ss", cultureinfo.invariantculture);       string format = "mm/dd/yyyy hh:mm:ss tt";     console.writeline(myreader["rxnumber"].tostring() + "\t\t" + dt.tostring(format)); } 

output:

connection opened

date before formatting = 2013/12/26 11:26:08

            12/26/2013 11:26:08 

date before formatting = 2013/12/26 09:02:01

12345 12/26/2013 09:02:01 am

date before formatting = 2013/12/26 09:04:29

123456 12/26/2013 09:04:29 am

date before formatting = 2013/10/28 10:19:26

            10/28/2013 10:19:26 

date before formatting = 2014/02/14 12:25:57

7000006 02/14/2014 12:25:57 am

date before formatting = 2014/02/14 13:20:18

system.formatexception: string not recognized valid datetime. @ system.datetimeparse.parseexact(string s, string format, datetimeformatinfo dtfi, datetimestyles style) @ system.datetime.parseexact(string s, string format, iformatprovider provider) @ test.program.main(string[] args) in c:\users\admin\desktop\software testing test608\test\test\program.cs:line 33 ☺press key continue . . .

your problem database storing values in 24-hour format , you're parsing them datetime 12-hour format; use instead:

datetime dt = datetime.parseexact(     myreader["date"].tostring(),     "yyyy/mm/dd hh:mm:ss",     cultureinfo.invariantculture); 

once date parsed database correctly, output 12-hour format fine.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -