2013-07-15 69 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 
相關問題