4

有了这个脚本

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.
4

3 回答 3

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);

演示: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);
})
于 2013-01-19T15:21:26.247 回答
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());
});

现场演示

于 2013-01-19T15:26:45.940 回答
1

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

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

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

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);
});
于 2013-01-19T15:25:16.510 回答