最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - convert time 12 hour format to 24 hour format and vice-versa - Stack Overflow

programmeradmin5浏览0评论

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

Example,

  • time is entered as 10_10_am result should be:-

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

  • time is entered as 10_10_pm result should be:-

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

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

Example,

  • time is entered as 10_10_am result should be:-

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

  • time is entered as 10_10_pm result should be:-

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

Share Improve this question edited Oct 10, 2013 at 1:18 Tushar Gupta - curioustushar 57.1k24 gold badges105 silver badges109 bronze badges asked Jul 16, 2013 at 10:43 user2586839user2586839 1
  • what have you done so far??Place your code in the question!!!its not that that difficult.Just create some custom conditions and apply logic accordingly. – HIRA THAKUR Commented Jul 16, 2013 at 10:45
Add a ment  | 

2 Answers 2

Reset to default 13

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

$(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/cse_tushar/xEuUR/

updated after Adrian P 's ment

Working Demo http://jsfiddle/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';
    }
}

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 ##_##_pm or ##_##_am you can split this string on every _ and grab first value as hours, second as minutes and pare the third and

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

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]
    }
}
发布评论

评论列表(0)

  1. 暂无评论