2

I am writing an web application in MVP. One issue I got into is, I am not sure how to convert the 24hr in the database to 12hr clock on the webpage. Here is the idea: The user enters time in 12hr clock, but the database saves it as 24hr clock. When the user click an record, the time in database should be converted back in 12hrs, and show on the webpage. There are three dropdown boxes (Hours, Mins, AM/PM). I have had the 12hr clock to 24hr clock part. Can anyone help me with the 24hr clock to 12hr clock part? I don't know how to return 20:00:00 to 8 : 00 PM, and set each value to three dropdown boxes. Here is my current code:

 public string sunOpenTime
    {
        get //Convert 12hr clock to 24 clock
        {
            int hours = 0;
            int mins = 0;
            hours = Convert.ToInt32(ddlSundayOpenTimeHr.Text);
            mins = Convert.ToInt32(ddlSundayOpenTimeMin.Text);
            TimeSpan ts;
            if (ddlSundayFrom.SelectedValue == "PM")
            {
                ts = new TimeSpan(hours + 12, mins, 0);
            }

            else
            {
                ts = new TimeSpan(hours, mins, 0);
            }
            return ts.ToString();
        }

        set // Not really sure what to do here
        {
            sunOpenTime = value; //SunOpenTime is made by two parts: ddlSundayOpenTimeHr.Text + ddlSundayOpenTimeMin.Text;
        }
    }

And for the AM/PM Dropdown:

public string ddSundayFrom
    {
        if ... // something should be added here to decide if the value is PM or AM
        ddSundayFrom = 'PM';
        else
        ddSundayFrom = 'AM';
    }

Any help or ideas would be appreciated. Thanks a lot!

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
002432SAM
  • 39
  • 7
  • 1
    Already solved. http://stackoverflow.com/questions/10123426/how-to-convert-24-hour-format-timespan-to-12-hour-format-timespan – dtucker1914 Oct 05 '15 at 19:47

4 Answers4

3

I just want to be clear about this. In .NET, a DateTime represents an instant in time. It is stored in memory in one specific way and your user interface, which is serving it up to the user can show it in many different ways. Just like if someone asks you for the time, you might say to them "It's 1400 hours" or you might say "It's 2pm" or you might say something like "It's a quarter after 3". You can answer the question many different ways.

Similarly, your User Interface can take a DateTime structure and display it many different ways:

DateTime myDate = new DateTime(2015, 10, 5, 17, 30, 00);
string twelveHour = myDate.ToString("h:mm tt"); // 5:30 PM
string twentyFourHour = myDate.ToString("HH:mm"); // 17:30

You can see more examples here. The thing to take away from this though is that you aren't "converting" the DateTime structure. What you are doing is composing a string, in a specific format, based on the DateTime.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
1

Use the DateTime structure. Someone else has already asked for something similar here :)

Community
  • 1
  • 1
Khaine775
  • 2,715
  • 8
  • 22
  • 51
1

If the time value is stored as a time, date or datetime in the database you can read it into a DateTime and use string formatting to determine how it will be displayed.

string timeStr = timevalue.ToString("h:mm tt");

In the format string above h is the 1- or 2-digit 12-hour time hours portion, mm is minutes and tt is the upper-case AM or PM value. If you want to break that up into pieces for your MVC page you can do this:

string[] timeParts = timevalue.ToString("h:mm tt").Split(':', ' ').ToArray();

This will give you an array with three parts: hours, minutes and AM/PM. These can then be loaded into the appropriate parts of the web form.

Much simpler than doing the number gymnastics yourself.

Corey
  • 15,524
  • 2
  • 35
  • 68
0

You can compare a value whether being larger

String meridiem = (hours > 0 && hours < 13) ? "AM" : "PM";

Then use the modulo operator to find out the 12hours time:

int hours12 = (hours > 0 && hours < 13) ? hours % 13 : hours - 12;

Edited to reflect the truth below.

  • 2
    The problem here is that 0 hundred hours in is 12 AM. – juharr Oct 05 '15 at 19:52
  • I think I fixed it but still unsure. :( – Victor - Reinstate Monica Oct 05 '15 at 20:00
  • 1
    Nope, now `hours12` will be -12 when `hours` is 0. You just need to handle the 3 cases of 0, 1-12, and 13-23. – juharr Oct 05 '15 at 20:03
  • Well, yes, and you could have 24:00 and 0:00 but only 0:01, etc.. ..grrrrr.. – Victor - Reinstate Monica Oct 05 '15 at 20:09
  • Probably best to assume that 24:00 is not possible. I don't know under what scenario it would be. But either way this would work `int hours12 = hours == 0 ? 12 : (hours > 12 ? hours -12 : hours);` – juharr Oct 05 '15 at 20:12
  • Unless you're working with 26 hour days you need to mod by 12. To get the range 1..12 as outputs you need to offset the starting value by -1, then add back after modulus. And to get 0 to come out as 12 you should add 12 to the starting value... which, combined, looks like this: `int hours12 = (hours + 11) % 12 + 1;` – Corey Oct 06 '15 at 02:19