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

xxls0lw8  于 2023-05-16  发布在  Java
关注(0)|答案(3)|浏览(95)

用这个剧本

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.第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.
fhg3lkii

fhg3lkii1#

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

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
编辑:将时间 Package 在标签中将大大简化情况。使用一个公共类将所有军事时间 Package 在一个span中,然后使用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);
})
myzjeezk

myzjeezk2#

使用这个:

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

dffbzjpn

dffbzjpn3#

你可以在正则表达式中使用word boundaries来匹配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);
});

相关问题