问题:转换12小时时钟到24小时,然后反向回 [英] Issue: Convert 12hr clock to 24 hrs, then reverse it back

查看:517
本文介绍了问题:转换12小时时钟到24小时,然后反向回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#初学者。我在写MVP的Web应用程序,并遇到了麻烦,以12小时时钟转换为24小时时钟。
所以,有三个下拉框(小时,分钟,AM / PM)
当AM / PM下拉列表显示为PM,小时值应该由12加这里是我的代码,但蹊跷的存在。

I am a c# beginner. I am writing a web application in MVP, and having trouble to convert 12 hr clock to 24 hr clock. So, there are three dropdown boxes (hour, mins, AM/PM) When the AM/PM dropdown shows up as PM, the value of hour should be added by 12. Here is my code, but something wrong there.

public string sunOpenTime
{
    get
    {
        if (ddlSundayFrom.SelectedValue == "PM")
            return sunOpenTime = (ddlSundayOpenTimeHr.Text += 12) + ddlSundayOpenTimeMin.Text;
        else
            return sunOpenTime = ddlSundayOpenTimeHr.Text + ddlSundayOpenTimeMin.Text;
    }

    set
    {
        sunOpenTime = value;
    }
}



另一个问题我已经是,当用户点击存在的记录时,PM / AM下拉应正确显示在网页上。 (如果在数据库中的记录是18点,那么它应该是6:00 PM网页上)
我不是很知道如何做到这一点。
谁能帮助我?非常感谢!

Another question I have is, when the user click the existed record, the PM/AM dropdown should show up correctly on the web page. (If the record in the database is 18:00, then it should be 6:00 PM on the web page) I am not very sure how to do that. Can anyone help me? Thanks a lot!!

推荐答案

下面是如何,你可以,如果时间是时间转换到24小时格式代码示例在PM。
你可以做反向的,即沿着相似的线24小时制12小时格式转换为下面的示例和放大器的;因此,留给你。 。如果您想要的任何帮助那里,让我知道

Below is a code sample of how you could convert the time to 24 hour format if the time is in PM. You could do the reverse, i.e. converting from 24 hours format to 12 hours format along similar lines as in the sample below & hence leave that to you. In case you want any help there, let me know.

代码示例:12小时〜24小时

public string sunOpenTime
{
    get
    {
        int hours = 0;
        int mins = 0;
        if (int.TryParse(ddlSundayOpenTimeHr.Text, hours) && int.TryParse(ddlSundayOpenTimeHr.Text, mins))
        {
            TimeSpan ts;
            if (ddlSundayFrom.SelectedValue == "PM")
            {
                ts = new TimeSpan(hours + 12, mins, 0);   
            }
            else
            {
                ts = new TimeSpan(hours, mins, 0);
            }
            return ts.ToString();//There are numerous ways to format the time string, check link below.
        }
        else
        {
                return "Invalid Time";//Indicatory msg - Handle it the way you want.
        }
    }

    set
    {
        sunOpenTime = value;
    }
}



PS:链接格式化时间字符串的这里

编辑1:

您可以考虑转换代码吸气移动(12小时至24小时)传递给函数或在适合你的任何代码块。从本质上讲,我试图从12小时只传达转换的方法到24小时。

You may consider moving the conversion code (12 hr to 24 hr) from the getter to a function or in any code block that suits you. In essence I'm trying to convey only the method of conversion from 12 hr to 24 hrs.

这篇关于问题:转换12小时时钟到24小时,然后反向回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆