Link to home
Get AccessLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

datetime in c#

We have something like 90000 as 9:00 am and 230000 as 11:00 pm
how to convert 90000 to 9:00 am and 230000 to 11:00 pm in c#?
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

try something like this:

long yourTime = 90000;
            string yourTimestr = yourTime.ToString().PadLeft(6, '0');

            DateTime t = new DateTime(1900, 1, 1, Convert.ToInt32(yourTimestr.ToString().Substring(0, 2)), Convert.ToInt32(yourTimestr.ToString().Substring(2, 2)), Convert.ToInt32(yourTimestr.ToString().Substring(4, 2)));

Open in new window

Avatar of ITsolutionWizard

ASKER

have an alert error message: Input string was not in a correct format from the following code.

Convert.ToInt64(educationClass.Element("Start_Time").ToString())
 public DateTime SpecialFormatDate(Int64 x)
        {
            long yourTime = x;
            string yourTimestr = yourTime.ToString().PadLeft(6, '0');
            DateTime t = new DateTime(1900, 1, 1, Convert.ToInt32(yourTimestr.ToString().Substring(0, 2)), Convert.ToInt32(yourTimestr.ToString().Substring(2, 2)), Convert.ToInt32(yourTimestr.ToString().Substring(4, 2)));
            return t;            
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
"90000"
sorry. my bad. it is 090000
if:

educationClass.Element("Start_Time").ToString()

Open in new window

equals to:

"090000 ", you could try:

public DateTime SpecialFormatDate(string yourTimestr)
        {
            DateTime t = new DateTime(1900, 1, 1, Convert.ToInt32(yourTimestr.Substring(0, 2)), Convert.ToInt32(yourTimestr.Substring(2, 2)), Convert.ToInt32(yourTimestr.Substring(4, 2)));
            return t;
        }

Open in new window

then to implement:

DateTime t = SpecialFormatDate(educationClass.Element("Start_Time").ToString());

Open in new window

Rather than converting back and forth with strings to get the hours, minutes, seconds you could use integer division and remainder for the same manipulation.
As the string may hold minutes and seconds(?), parse it:

string textTime = "231000";

System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;

DateTime datetime = DateTime.ParseExact(textTime, "HHmmss", provider);
TimeSpan timespan = TimeSpan.ParseExact(textTime, "hhmmss", provider);

provider = System.Globalization.CultureInfo.GetCultureInfo("en-us");
Console.WriteLine(datetime.ToString("t", provider));
Console.WriteLine(timespan.ToString());

Open in new window

Output:

11:10 PM
23:10:00

Open in new window

Just a note that Gustav's DateTime.Parse solution fails on "90000". You would need to pad the source string before attempting to parse it:

DateTime datetime = DateTime.ParseExact(textTime.PadLeft(6, '0'), "HHmmss", provider);

Open in new window