javascript 将时间 12 小时格式转换为 24 小时格式,反之亦然

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17674463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:09:37  来源:igfitidea点击:

convert time 12 hour format to 24 hour format and vice-versa

javascriptjqueryregexstringtime

提问by Tushar Gupta - curioustushar

I want a js script that converts inputted time to 24 hour format or 12 hour format.

我想要一个将输入时间转换为 24 小时格式或 12 小时格式的 js 脚本。

Example,

例子,

  • time is entered as 10_10_amresult should be:-

    10:10 AM(12 hr format) and 10:10(24 hr format)

  • time is entered as 10_10_pmresult should be:-

    10:10 PM(12 hr format) and 22:10(24 hr format)

  • 输入时间,10_10_am结果应为:-

    10:10 AM(12 小时制)和10:10(24 小时制)

  • 输入时间,10_10_pm结果应为:-

    10:10 PM(12 小时制)和22:10(24 小时制)

回答by Tushar Gupta - curioustushar

HTML

HTML

<input type="text" id="textbox1"/>
<input type="button" id="b1" value="convert 12 hr"/>
<input type="button" id="b2" value="convert 24 hr"/>
<div id="result"></div>

JS

JS

$(document).ready(function () {
    function am_pm_to_hours(time) {
        console.log(time);
        var hours = Number(time.match(/^(\d+)/)[1]);
        var minutes = Number(time.match(/:(\d+)/)[1]);
        var AMPM = time.match(/\s(.*)$/)[1];
        if (AMPM == "pm" && hours < 12) hours = hours + 12;
        if (AMPM == "am" && hours == 12) hours = hours - 12;
        var sHours = hours.toString();
        var sMinutes = minutes.toString();
        if (hours < 10) sHours = "0" + sHours;
        if (minutes < 10) sMinutes = "0" + sMinutes;
        return (sHours +':'+sMinutes);
    }

    function hours_am_pm(time) {
        var hours = time[0] + time[1];
        var min = time[2] + time[3];
        if (hours < 12) {
            return hours + ':' + min + ' AM';
        } else {
            hours=hours - 12;
            hours=(hours.length < 10) ? '0'+hours:hours;
            return hours+ ':' + min + ' PM';
        }
    }
    $('#b1').click(function(){
         var n = $('#textbox1').val();
         var n1 =n.split('_');
         var time = hours_am_pm(n1[0]+n1[1]);
        $('#result').text(time);
    }); 
    $('#b2').click(function(){
        var n = $('#textbox1').val();
         var n1 =n.split('_');
        var time = am_pm_to_hours(n1[0]+':'+n1[1]+' '+n1[2]);
        $('#result').text(time);
    });

});

Working Demo http://jsfiddle.net/cse_tushar/xEuUR/

工作演示http://jsfiddle.net/cse_tushar/xEuUR/

updated after Adrian P 's comment

在 Adrian P 的评论后更新

Working Demo http://jsfiddle.net/cse_tushar/xEuUR/4

工作演示http://jsfiddle.net/cse_tushar/xEuUR/4

function hours_am_pm(time) {
    var hours = Number(time.match(/^(\d+)/)[1]);
    var min =  Number(time.match(/:(\d+)/)[1]);
    if (min < 10) min = "0" + min;
    if (hours < 12) {
        return hours + ':' + min + ' AM';
    } else {
        hours=hours - 12;
        hours=(hours < 10) ? '0'+hours:hours;
        return hours+ ':' + min + ' PM';
    }
}

回答by user2227904

I am not sure if there is any specific function that already exists, but this is fairly easy to write.

我不确定是否已经存在任何特定的函数,但这很容易编写。

Considering your input is always ##_##_pmor ##_##_amyou can split this string on every _and grab first value as hours, secondas minutesand compare the third and

考虑到您的输入总是##_##_pm或者##_##_am您可以在每个上拆分此字符串_并获取第一个值 as hours, secondasminutes并比较第三个和

if it's pm add 12 hoursto the hours variable for 24 hr format. You need a function that takes 2 parameters (format and string) It will look something like this:

如果是 pm 添加12 hours到小时变量中24 hr format。您需要一个带有 2 个参数(格式和字符串)的函数,它看起来像这样:

function timeFormat(format, str){
    var timeParts=str.split("_");
    if(format==12){
       return timeParts[0] + ":" + timeParts[1] + " " + timeParts[2];
    }else if(format == 24){
       var hours = timeParts[0];
       if(timeParts[2] == "pm")
           hours += 12;
       return hours + ":" + timeParts[1]
    }
}