Thursday, March 22, 2012

taking the time now

how to i take the time now and convert it to string?
and also the other way round.

thanks..to string:

DateTime.Now.ToString()
(see overloads as well)

From string:
DateTime.Parse
DateTime.ParseExact
(see overloads as well)
i only wan the time.. i don't need the date.
Internally DateTime always stores both, so you can't avoid that when parsing from string but outputting to string you can try:

DateTime.Now.ToLongTimeString()
DateTime.Now.ToShortTimeString()

The ToString I gave earlier is perfectly valid for this as well as you output the time using string formatting

DateTime.Now.ToString("hh:mm:ss")
i tried the DateTime.Now.ToString("hh:mm:ss")
but it give me the wrong AM/PM.. how come?
Ah, it shows with 12-hour clock. Try this: "H:mm:ss"

Indeed see .NET Framework SDK for string formatting. The syntax is explained there.

0 comments:

Post a Comment