Tony Borf
Tony Borf

Reputation: 4660

Converting String to time and back again

I am converting a fractional hour value in this format '00.000' to a hour and minute format. So that '02.500' will be '02:30' and I also have to do this in reverse.

I built a test to see how this will work,

<%
    TimeSpan tspan;
    TimeSpan tspan2;   

     string TimeString;
     string TimeString2;
     string Converted;
     double ConvTime;
     for (int HH = 0; HH < 24; HH++)
     {
         for (int M1 = 0; M1 < 6; M1++)
         {
             for (int M2 = 0; M2 < 10; M2++)
             {
                 TimeString = HH.ToString() + ":" + M1.ToString() + M2.ToString();

                 // Convert Time String to Fractional Hours 
                 tspan = TimeSpan.Parse(TimeString);
                 ConvTime = Convert.ToDouble(tspan.TotalHours);
                 Converted = String.Format("{0:00.000}", ConvTime);

                 // Convert Fractional Hour String to Time String
                 tspan2 = TimeSpan.FromHours(Convert.ToDouble(Converted));
                 TimeString2 = string.Format("{0:D1}:{1:D2}", tspan2.Hours, tspan2.Minutes);


                 Response.Write(TimeString + " = ");
                 Response.Write(Converted + " = ");
                 Response.Write(TimeString2 + "<br>");
             }
         }
     }     
%> 

The problem I am having is that when I convert to a string I loose the remainder form the converstion, which throws off the value when converting back.

Here is the output for the first 10 seconds.

0:00 = 00.000 = 0:00
0:01 = 00.017 = 0:01
0:02 = 00.033 = 0:01
0:03 = 00.050 = 0:03
0:04 = 00.067 = 0:04
0:05 = 00.083 = 0:04
0:06 = 00.100 = 0:06
0:07 = 00.117 = 0:07
0:08 = 00.133 = 0:07
0:09 = 00.150 = 0:09
0:10 = 00.167 = 0:10

as you can see what I started with is not always what I end up with.

How can I correctly convert a time value string "HH:MM" to a fractional string "00.000" and back again correctly.

Thanks

EDIT:

I tried this,

 TimeString = "02:02";
 Converted = TimeSpan.Parse(TimeString).TotalHours.ToString("00.000");

 tspan2 = TimeSpan.FromHours(Convert.ToDouble(Converted));
 TimeString2 = string.Format("{0:D1}:{1:D2}", tspan2.Hours, tspan2.Minutes);

But I get Converted = "02.033" and TimeString2 = "2:01". It should equal "2:02". I'm thinking that I'm loosing the remainder when I force it to "00.000".

Upvotes: 3

Views: 9999

Answers (3)

Meta-Knight
Meta-Knight

Reputation: 17845

//Parse both strings to TimeSpan
var ts = TimeSpan.FromHours(Double.Parse("02.500"));   
var ts2 = TimeSpan.Parse("02:30");

//Convert a TimeSpan to string format
var firstFormat = ts.Hours.ToString.PadLeft(2, '0') 
                  + ":" + ts.Minutes.ToString.PadLeft(2, '0');
//Result: 02:30
var secondFormat = ts.TotalHours.ToString("00.000");
//Result: 02.500

Upvotes: 1

M4N
M4N

Reputation: 96551

To convert a string to a hours, use this:

string time = "1:30";
double hours = TimeSpan.Parse(time).TotalHours; // -> 1.5

For the reverse, you can use this:

double hours = 1.5;
string time = TimeSpan.FromHours(hours).ToString(); // -> "00:01:30"

Upvotes: 8

Mark Seemann
Mark Seemann

Reputation: 233125

Why don't you just use DateTime.Parse?

Upvotes: 7

Related Questions