Replace military time to normal time with Javascript

后端 未结 3 1469
伪装坚强ぢ
伪装坚强ぢ 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条回答
  • 2020-12-10 09:05

    Use this:

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

    Output:

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

    Update

    In your case:

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

    Update 2

    If you have the time inside <td class="fourDigitTime">1500</td>, then use this:

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

    Live Demo

    0 讨论(0)
  • 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 <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);
    });
    
    0 讨论(0)
  • 2020-12-10 09:14

    Assuming the only digits displayed in the text are the times you can use:

    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

    EDIT: Wrapping times in a tag would greatly simplify situation. Wrap all military times in a span with a common class and then use the html() method

    <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);
    })
    
    0 讨论(0)
提交回复
热议问题