2

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?

Evil August
  • 411
  • 6
  • 18

6 Answers6

1

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.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Also, I was getting an exception with ParseExact, so I am just doing DateTime.Parse(date). It is giving me the correct DateTime values. – Evil August Dec 23 '13 at 14:42
  • 1
    If you were getting an exception, then you probably have data that doesn't match the format supplied. Now you are probably hiding the problem. Be careful. – Matt Johnson-Pint Dec 23 '13 at 17:33
1

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);
}
Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
0

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();
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
0

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()

Klaus Nji
  • 18,107
  • 29
  • 105
  • 185
0

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

hanskishore
  • 361
  • 1
  • 3
  • 11
0

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

Ravi Ram
  • 24,078
  • 21
  • 82
  • 113