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 に答える