CS Lewis
CS Lewis

Reputation: 489

Double Data Type representation of 24 hour time to the 24-hour Format time

All:

Here are the details associated with my development environment:

In my MongoDB Database, there is collection called BusSchedule with the following fields

BusSchedule
-Double StartTime
-Double EndTime
-String 24hourFormatStartTime
-String 24hourFormatEndTime

Here are some typical values that might exist

StartTime = 6.5
EndTime = 9.5
24hourFormatStartTime = "06:30"
24hourFormatEndTime = "09:30"

StartTime = 8.5
EndTime = 14.25
24hourFormatStartTime = "08:30"
24hourFormatEndTime = "14:15"

I was wondering if Microsoft had some kind of existing library that could easily convert the Double Data Type representation of 24 hour time to the 24-hour Format time.

StartTime = 8.5  -------translated to-----> 24hourFormatStartTime = "08:30"
EndTime = 14.25  -------translated to-----> 24hourFormatEndTime = "14:15"

StartTime = 6.5 -------translated to-----> 24hourFormatStartTime = "06:30"
EndTime = 9.5 -------translated to-----> 24hourFormatEndTime = "09:30"

I don't want to waste time writing code to do the aforementioned translation. Could someone please tell me if Microsoft or some 3rd-party open source library will handle the aforementioned requirement?

Upvotes: 0

Views: 499

Answers (1)

Soner Gönül
Soner Gönül

Reputation: 98750

I already can read the StartTime and EndTime values from MongoDB and place them in double variables. how can I convert it to a string version of 24-hour Format time?

Then you can use this double values to generate a TimeSpan and get their string representation using TimeSpan.ToString() method andhh\\:mm format.

TimeSpan ts = TimeSpan.FromHours(14.25);

enter image description here

Then

Console.WriteLine(ts.ToString("hh\\:mm")); // 14:15

Be aware, this code is for .Net 4.0 or above. If you have .Net 3.5 and below, you need to use string.Format as explained.

Upvotes: 1

Related Questions