2013-07-15 36 views
1

我得到一个变量的字符串,像这样:变化时间字符串:MM AM/PM 24小时的时间

上午8:45

而想,如果是下午,将其转换为24小时的时间。所以我可以放下am/pm并用其他的东西来使用它。

我可以很容易掉落的AM/PM是这样的:

function replaceEnds(string) { 
     string = string.replace("am", ""); 
     string = string.replace("pm", ""); 
     return string; 
    } 

但是,当然,如果我这样做,我不知道是否该字符串是上午还是下午,所以我不知道在字符串上添加12小时以使其24小时。

任何人都知道我可以解决这个问题吗?我绝对不能改变我得到的变量输入,它始终是小时(12小时内),分钟,上午或下午。

回答

5

使用moment.js

moment(string, 'h:mm a').format('H:mm'); 

如果你想要做手工,这将是我的解决方案:

function to24Hour(str) { 
    var tokens = /([10]?\d):([0-5]\d) ([ap]m)/i.exec(str); 
    if (tokens == null) { return null; } 
    if (tokens[3].toLowerCase() === 'pm' && tokens[1] !== '12') { 
     tokens[1] = '' + (12 + (+tokens[1])); 
    } else if (tokens[3].toLowerCase() === 'am' && tokens[1] === '12') { 
     tokens[1] = '00'; 
    } 
    return tokens[1] + ':' + tokens[2]; 
} 

的手动解决方案是很难理解的,不够灵活,缺少一些错误检查并需要单元测试。一般来说,你通常应该选择一个经过充分测试的热门图书馆解决方案,而不是你自己的解决方案(如果经过充分测试的图书馆可用)。

+0

快速,方便,谢谢:)会选择在2分钟内回答 –

0

我有类似这样

//time is an array of [hh] & [mm am/pm] (you can get this by time = time.split(":"); 
function MilitaryTime(time){ 
if(time[1].indexOf("AM")!=-1){ 
//its in the morning, so leave as is 
    return time; 
}else if(time[0]!="12"){ 
//If it is beyond 12 o clock in the after noon, add twelve for military time. 
    time[0]=String(parseInt(time[0])+12); 
    return time; 
} 
else{ 
return time; 
} 
} 

一旦你让你的时间回来,你可以改变任何你想要的文字使用的东西。

+1

此解决方案并没有考虑到上午12:00 - > 00:00,而parseInt函数将无法在旧版浏览器(如firefox 3.6)输入'09'。请参阅http://jsfiddle.net/j2xc6/1/。 - >点击小提琴上的jshint按钮来查看原因。 – forivall

+0

啊,当它!这是我从内存编码时发生的情况。谢谢你的收获,下次还会记得。 – ElliotM

5

不使用任何额外的JavaScript库:

/** 
* @var amPmString - Time component (e.g. "8:45 PM") 
* @returns - 24 hour time string 
*/ 
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" 
1

如果你正在寻找的任何格式转换至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 
相关问题