Evil August
Evil August

Reputation: 410

How do I convert to DateTime when the time is a decimal?

I have a data table, from Sage 100 ERP to be specific, that uses two separate columns to store the updated date and time (DateUpdated and TimeUpdated). I need to convert the data in these two fields to a DateTime object for comparisons. The dates look like "mm/dd/yyyy 12:00 AM" and the time is a decimal like 14.29297. So far I have been able to convert the time to the minute as follows:

private DateTime GetDateTime(string date, decimal time)
    {
        int hour = int.Parse(Math.Floor(time).ToString());
        decimal minTemp = decimal.Parse((60 * (time - hour)).ToString());
        int min = int.Parse(Math.Round(minTemp).ToString());
        int sec = int.Parse(Math.Round(60 * (minTemp - min)).ToString());

        string datetime = date + " " + hour.ToString() + ":" + min.ToString();

        return DateTime.Parse(datetime);
    }

I remove the 12:00AM from the date string before I pass it to this method. This works, but I'm loosing the seconds which is very important.

How can I convert the time to hours, minutes, and seconds?

Upvotes: 2

Views: 6814

Answers (6)

Ravi Ram
Ravi Ram

Reputation: 24488

Covert Decimal to Hour and Minute for single Database field

        double convertData = 1.75
        TimeSpan timespan = TimeSpan.FromHours(convertData);
        string outputH = timespan.ToString("hh");
        string outputM = timespan.ToString("mm");

Output will be 1 hour / 45 min

Upvotes: 0

Cory Nelson
Cory Nelson

Reputation: 29981

It looks like you could avoid all that extra processing and just do this:

DateTime GetDateTime(string date, decimal time)
{
    return DateTime.Parse(datetime).AddHours((double)time);
}

Upvotes: 1

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241485

Just parse what you have:

private DateTime GetDateTime(string date, decimal time)
{
    DateTime dt = DateTime.ParseExact(date, "MM/dd/yyyy hh:mm tt",
                                      CultureInfo.InvariantCulutre);

    double hours = Convert.ToDouble(time);

    return dt.AddHours(hours);
}

Of course, it would be a lot easier if you had the correct data types to begin with. I have a feeling somewhere you have a DateTime and a double anyway (perhaps when you read the data from the database), and you are improperly converting them to string and decimal.

Upvotes: 1

hanskishore
hanskishore

Reputation: 361

For the seconds try something like this:

int secTemp = int.Parse((Math.Round(60 * (minTemp - min))).ToString());
int sec = (secTemp<0?60 + secTemp:secTemp);
string datetime = date + " " + hour.ToString() + ":" + min.ToString()+ ":" + sec.ToString();

Hope it helps

Upvotes: 0

Klaus Nji
Klaus Nji

Reputation: 18857

Assuming your calculations are correct, did you try appending the second component to the datetime variable as follows:

string datetime = date + " " + hour.ToString() + ":" + min.ToString() + ":" + sec.ToString()

Upvotes: 0

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

if it's not a typo - you are missing to append seconds value to the datetime string.

Replace This:

string datetime = date + " " + hour.ToString() + ":" + min.ToString();

With this:

string datetime = date + " " + hour.ToString() + ":" + min.ToString()+ ":" + sec.ToString();

Upvotes: 0

Related Questions