将12小时hh:mm上午/下午转换为24小时hh:mm

是否有任何简单的方法来转换12小时hh:毫米上午/下午至24小时hh:毫米使用jQuery?

注意:不要使用其他库。

我有一个var time = $("#starttime").val()返回一个hh:mm AM / PM。

尝试这个

 var time = $("#starttime").val(); 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; alert(sHours + ":" + sMinutes); 

我不得不做类似的事情,但我正在生成一个Date对象,所以我结束了这样的function:

 function convertTo24Hour(time) { var hours = parseInt(time.substr(0, 2)); if(time.indexOf('am') != -1 && hours == 12) { time = time.replace('12', '0'); } if(time.indexOf('pm') != -1 && hours < 12) { time = time.replace(hours, (hours + 12)); } return time.replace(/(am|pm)/, ''); } 

我觉得这个读起来更容易一些。 您input格式为h:mm am / pm的string。

  var time = convertTo24Hour($("#starttime").val().toLowerCase()); var date = new Date($("#startday").val() + ' ' + time); 

例子:

  $("#startday").val('7/10/2013'); $("#starttime").val('12:00am'); new Date($("#startday").val() + ' ' + convertTo24Hour($("#starttime").val().toLowerCase())); Wed Jul 10 2013 00:00:00 GMT-0700 (PDT) $("#starttime").val('12:00pm'); new Date($("#startday").val() + ' ' + convertTo24Hour($("#starttime").val().toLowerCase())); Wed Jul 10 2013 12:00:00 GMT-0700 (PDT) $("#starttime").val('1:00am'); new Date($("#startday").val() + ' ' + convertTo24Hour($("#starttime").val().toLowerCase())); Wed Jul 10 2013 01:00:00 GMT-0700 (PDT) $("#starttime").val('12:12am'); new Date($("#startday").val() + ' ' + convertTo24Hour($("#starttime").val().toLowerCase())); Wed Jul 10 2013 00:12:00 GMT-0700 (PDT) $("#starttime").val('3:12am'); new Date($("#startday").val() + ' ' + convertTo24Hour($("#starttime").val().toLowerCase())); Wed Jul 10 2013 03:12:00 GMT-0700 (PDT) $("#starttime").val('9:12pm'); new Date($("#startday").val() + ' ' + convertTo24Hour($("#starttime").val().toLowerCase())); Wed Jul 10 2013 21:12:00 GMT-0700 (PDT) 

这个问题需要一个更新的答案:)

 function convertTime12to24(time12h) { const [time, modifier] = time12h.split(' '); let [hours, minutes] = time.split(':'); if (hours === '12') { hours = '00'; } if (modifier === 'PM') { hours = parseInt(hours, 10) + 12; } return hours + ':' + minutes; } console.log(convertTime12to24('01:02 PM')); console.log(convertTime12to24('05:06 PM')); console.log(convertTime12to24('12:00 PM')); console.log(convertTime12to24('12:00 AM')); 

这里我的解决scheme包括秒:

 function convert_to_24h(time_str) { // Convert a string like 10:05:23 PM to 24h format, returns like [22,5,23] var time = time_str.match(/(\d+):(\d+):(\d+) (\w)/); var hours = Number(time[1]); var minutes = Number(time[2]); var seconds = Number(time[3]); var meridian = time[4].toLowerCase(); if (meridian == 'p' && hours < 12) { hours += 12; } else if (meridian == 'a' && hours == 12) { hours -= 12; } return [hours, minutes, seconds]; }; 

我需要一个项目的这个function。 我尝试了devnull69的,但我有一些麻烦,主要是因为stringinput是非常具体的am / pm部分,我会需要改变我的validation。 我与Adrian P.的jsfiddle搞混了,最后发布了一个版本,这个版本对于更多的date格式似乎更好。 这是小提琴: http : //jsfiddle.net/u91q8kmt/2/ 。

这是function:

 function ConvertTimeformat(format, str) { var hours = Number(str.match(/^(\d+)/)[1]); var minutes = Number(str.match(/:(\d+)/)[1]); var AMPM = str.match(/\s?([AaPp][Mm]?)$/)[1]; var pm = ['P', 'p', 'PM', 'pM', 'pm', 'Pm']; var am = ['A', 'a', 'AM', 'aM', 'am', 'Am']; if (pm.indexOf(AMPM) >= 0 && hours < 12) hours = hours + 12; if (am.indexOf(AMPM) >= 0 && 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; if (format == '0000') { return (sHours + sMinutes); } else if (format == '00:00') { return (sHours + ":" + sMinutes); } else { return false; } } 

这将有助于:

  function getTwentyFourHourTime(amPmString) { var d = new Date("1/1/2013 " + amPmString); return d.getHours() + ':' + d.getMinutes(); } 

例如:

 getTwentyFourHourTime("8:45 PM"); // "20:45" getTwentyFourHourTime("8:45 AM"); // "8:45" 

更新:注意:“时间”和“上午/下午”之间应该有时间string的空间。

基于会议CodeSkill#1

格式的validation应该是另一个function:)

 function convertTimeFrom12To24(timeStr) { var colon = timeStr.indexOf(':'); var hours = timeStr.substr(0, colon), minutes = timeStr.substr(colon+1, 2), meridian = timeStr.substr(colon+4, 2).toUpperCase(); var hoursInt = parseInt(hours, 10), offset = meridian == 'PM' ? 12 : 0; if (hoursInt === 12) { hoursInt = offset; } else { hoursInt += offset; } return hoursInt + ":" + minutes; } console.log(convertTimeFrom12To24("12:00 AM")); console.log(convertTimeFrom12To24("12:00 PM")); console.log(convertTimeFrom12To24("11:00 AM")); console.log(convertTimeFrom12To24("01:00 AM")); console.log(convertTimeFrom12To24("01:00 PM")); 

我已经创build了一个脚本@ devnull69提交的改编。 我觉得我的应用程序将作为一个函数返回我可以,然后用作一个variables更有用。

HTML

 <input type="text" id="time_field" /> <button>Submit</submit> 

jQuery的

 $(document).ready(function(){ function convertTime(time) { var hours = Number(time.match(/^(\d\d?)/)[1]); var minutes = Number(time.match(/:(\d\d?)/)[1]); var AMPM = time.match(/\s(.AM|PM)$/i)[1]; if (AMPM == 'PM' || AMPM == 'pm' && hours<12) { hours = hours+12; } else if (AMPM == 'AM' || AMPM == "am" && hours==12) { hours = hours-12; } var sHours = hours.toString(); var sMinutes = minutes.toString(); if(hours<10) { sHours = "0" + sHours; } else if(minutes<10) { sMinutes = "0" + sMinutes; } return sHours + ":" + sMinutes; } $('button').click(function(){ alert(convertTime($('#time_field').val())); }); 
  function getDisplayDatetime() { var d = new Date("February 04, 2011 19:00"), hh = d.getHours(), mm = d.getMinutes(), dd = "AM", h = hh; mm=(mm.toString().length == 1)? mm = "0" + mm:mm; h=(h>=12)?hh-12:h; dd=(hh>=12)?"PM":"AM"; h=(h == 0)?12:h; var textvalue=document.getElementById("txt"); textvalue.value=h + ":" + mm + " " + dd; } </script> </head> <body> <input type="button" value="click" onclick="getDisplayDatetime()"> <input type="text" id="txt"/> 
 function convertTo24Hour(time) { time = time.toUpperCase(); var hours = parseInt(time.substr(0, 2)); if(time.indexOf('AM') != -1 && hours == 12) { time = time.replace('12', '0'); } if(time.indexOf('PM') != -1 && hours < 12) { time = time.replace(hours, (hours + 12)); } return time.replace(/(AM|PM)/, ''); } 
 date --date="2:00:01 PM" +%T 14:00:01 date --date="2:00 PM" +%T | cut -d':' -f1-2 14:00 var="2:00:02 PM" date --date="$var" +%T 14:00:02 

你可以尝试这个更通用的function:

 function from12to24(hours, minutes, meridian) { let h = parseInt(hours, 10); const m = parseInt(minutes, 10); if (meridian.toUpperCase() === 'PM') { h = (h !== 12) ? h + 12 : h; } else { h = (h === 12) ? 0 : h; } return new Date((new Date()).setHours(h,m,0,0)); } 

注意它使用了一些ES6function。

  var time = "9:09:59AM" var pmCheck =time.includes("PM"); var hrs=parseInt(time.split(":")[0]); var newtime=''; // this is for between 12 AM to 12:59:59AM = 00:00:00 if( hrs == 12 && pmCheck == false){ newtime= "00" +':'+ time.split(":")[1] +':'+ time.split(":")[2].replace("AM",''); } //this is for between 12 PM to 12:59:59 =12:00:00 else if (hrs == 12 && pmCheck == true){ newtime= "12" +':'+ time.split(":")[1] +':'+ time.split(":")[2].replace("PM",''); } //this is for between 1 AM and 11:59:59 AM else if (!pmCheck){ newtime= hrs +':'+ time.split(":")[1] +':'+ time.split(":")[2].replace("AM",''); } //this is for between 1 PM and 11:59:59 PM else if(pmCheck){ newtime= (hrs +12)+':'+ time.split(":")[1] +':'+ time.split(":")[2].replace("PM",''); } console.log(newtime); 
 function timeConversion(s) { var time = s.toLowerCase().split(':'); var hours = parseInt(time[0]); var _ampm = time[2]; if (_ampm.indexOf('am') != -1 && hours == 12) { time[0] = '00'; } if (_ampm.indexOf('pm') != -1 && hours < 12) { time[0] = hours + 12; } return time.join(':').replace(/(am|pm)/, ''); } 

用string参数调用函数:

 timeConversion('17:05:45AM') 

要么

 timeConversion('07:05:45PM') 

万一你正在寻找一个解决scheme,将任何格式转换为24小时HH:MM正确。

 function get24hTime(str){ str = String(str).toLowerCase().replace(/\s/g, ''); var has_am = str.indexOf('am') >= 0; var has_pm = str.indexOf('pm') >= 0; // first strip off the am/pm, leave it either hour or hour:minute str = str.replace('am', '').replace('pm', ''); // if hour, convert to hour:00 if (str.indexOf(':') < 0) str = str + ':00'; // now it's hour:minute // we add am/pm back if striped out before if (has_am) str += ' am'; if (has_pm) str += ' pm'; // now its either hour:minute, or hour:minute am/pm // put it in a date object, it will convert to 24 hours format for us var d = new Date("1/1/2011 " + str); // make hours and minutes double digits var doubleDigits = function(n){ return (parseInt(n) < 10) ? "0" + n : String(n); }; return doubleDigits(d.getHours()) + ':' + doubleDigits(d.getMinutes()); } console.log(get24hTime('6')); // 06:00 console.log(get24hTime('6am')); // 06:00 console.log(get24hTime('6pm')); // 18:00 console.log(get24hTime('6:11pm')); // 18:11 console.log(get24hTime('6:11')); // 06:11 console.log(get24hTime('18')); // 18:00 console.log(get24hTime('18:11')); // 18:11 
 dateFormat.masks.armyTime= 'HH:MM'; now.format("armyTime"); 

我必须推荐一个图书馆: 时刻

码:

 var target12 = '2016-12-08 9:32:45 PM'; console.log(moment(target12, 'YYYY-MM-DD h:m:s A').format('YYYY-MM-DD HH:mm:ss'));