用Javascript将军事时间替换为正常时间

时间:2013-01-19 15:02:20

标签: javascript

使用此脚本

getFormattedTime = function (fourDigitTime){
var hours24 = parseInt(fourDigitTime.substring(0,2));
var hours = ((hours24 + 11) % 12) + 1;
var amPm = hours24 > 11 ? 'pm' : 'am';
var minutes = fourDigitTime.substring(2);

return hours + ':' + minutes + amPm;
};

我可以将4位数字时间更改为正常时钟时间(0930虽然存在问题...)

用这个

$("body").html($("body").html().replace(/1135/g,'11:35am'));

替换我页面中的任何1135事件。

但是,在我的页面中,我有一个表格中的时间列表。我需要转换它们,例如

Class starts at 1700, please be there by 1630 and sign in by 1645.

它应该转化为

Class starts at 05:00pm, please be there by 04:30pm and sign in by 04:45pm.

3 个答案:

答案 0 :(得分:6)

假设文本中显示的唯一数字是您可以使用的次数:

var txt = 'Class starts at 0845, please be there by 1630 and sign in by 1645.'

getFormattedTime = function (fourDigitTime) {
    var hours24 = parseInt(fourDigitTime.substring(0, 2),10);
    var hours = ((hours24 + 11) % 12) + 1;
    var amPm = hours24 > 11 ? 'pm' : 'am';
    var minutes = fourDigitTime.substring(2);

    return hours + ':' + minutes + amPm;
};
/* replace numeric entities*/
var newTxt = txt.replace(/(\d+)/g, function (match) {
    return getFormattedTime(match)
})
$('body').html(newTxt);

DEMO:http://jsfiddle.net/q6HC9/1

编辑:标签中的包装时间将大大简化情况。使用公共类包裹所有军事时间,然后使用html()方法

<span class="mil_time">0845</span>
getFormattedTime = function (fourDigitTime) {
    /* make sure add radix*/
    var hours24 = parseInt(fourDigitTime.substring(0, 2),10);
    var hours = ((hours24 + 11) % 12) + 1;
    var amPm = hours24 > 11 ? 'pm' : 'am';
    var minutes = fourDigitTime.substring(2);

    return hours + ':' + minutes + amPm;
};
/* find all spans and replace their content*/
$('span.mil_time').html(function( i, oldHtml){
   return getFormattedTime(oldHtml);
})

答案 1 :(得分:2)

使用此:

var getFormattedTime = function (fourDigitTime){
    var hours24 = parseInt(fourDigitTime.substring(0,2), 10);
    var hours = ((hours24 + 11) % 12) + 1;
    var amPm = hours24 > 11 ? 'pm' : 'am';
    var minutes = fourDigitTime.substring(2);

    return hours + ':' + minutes + amPm;
};

s = "Class starts at 1700, please be there by 1630 and sign in by 1645.";

c = s.replace(/([^\d]*)([0-9]{4})([^\d]*)/g, function(match, p1, p2, p3) {
    return p1 + getFormattedTime(p2) + p3
});

console.log(c);

输出:

Class starts at 5:00pm, please be there by 4:30pm and sign in by 4:45pm. 

<强>更新

在你的情况下:

s = $("body").html();

c = s.replace(/([^\d]*)([0-9]{4})([^\d]*)/g, function(match, p1, p2, p3) {
    return p1 + getFormattedTime(p2) + p3
});

$("body").html(c);

更新2

如果你有<td class="fourDigitTime">1500</td>的时间,请使用:

$(".fourDigitTime").each(function() {
    $(this).text(getFormattedTime($(this).text());
});

Live Demo

答案 2 :(得分:1)

您可以在正则表达式中使用word boundaries来匹配4位数字,然后将getFormattedTime函数用作.replace的替换函数:

$('body').html(function(_, old) {
    return old.replace(/\b\d{4}\b/g, getFormattedTime);
});

请注意@Doorknob和@Pointy的评论。要仅替换时间“数字”,您需要在语义上对其进行标记,例如使用html5 <time> tags

Class starts at <time>1700</time>, please be there by <time>1630</time> and sign in by <time>1645</time>.
$('time').text(function(_, old) {
    return getFormattedTime(old);
});