0

All:

Here are the details associated with my development environment:

  • Microsoft Visual Studio Professional 2013
  • MongoDB version 3.0.0

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?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
CS Lewis
  • 489
  • 8
  • 30
  • 1
    You can read these values on C# side as `System.Double` or `System.String`? – Soner Gönül Nov 02 '15 at 08:13
  • The Double type is associated with StartTime and EndTime on the C# side. The 24hourFormatStartTime and 24hourFormatEndTime are associated with String on the C# side. Am I answering your question properly? – CS Lewis Nov 02 '15 at 08:18
  • 1
    I know their associations but _can_ you read this `6.5` or `14.25` values from your mongodb and assign it C# double variable like `/* read this value from db */ double d = 14.25`? Or are you asking how to read this values from mongodb and assign it? – Soner Gönül Nov 02 '15 at 08:21
  • 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? – CS Lewis Nov 02 '15 at 08:35

1 Answers1

1

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.

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364