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!