有了这行代码
$("body").html($("body").html().replace(/1800/g,'6:00pm'));
我可以替换这个词,但我怎样才能用这个脚本让它更有活力呢?
var 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;
};
我的 HTML 代码是例如
Your class will start at 1900, please be there by 1845.
将计入哪个
Your class will start at 07:00pm, please be there by 06:45pm.
我试过这个:
$("body").html($("body").html().replace(fourDigitTime, getFormattedTime(fourDigitTime)));
var fourDigitTime = document.getElementById('fourDigitTime').value;
var 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;
};
这没用..