Replace military time to normal time with Javascript

后端 未结 3 1486
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 08:23

With this script

getFormattedTime = function (fourDigitTime){
var hours24 = parseInt(fourDigitTime.substring(0,2));
var hours = ((hours24 + 11) % 12) + 1;
va         


        
3条回答
  •  猫巷女王i
    2020-12-10 09:09

    You can use word boundaries in your regular expression to match 4 digits, and then your getFormattedTime function as a replacement function to .replace:

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

    Please notice also the comments by @Doorknob and @Pointy. To replace only time "numbers", you will need to markup them semantically, for example with html5

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

提交回复
热议问题