我想用小时分钟和安培分开一个时间字符串。
当我尝试使用以下代码时的英语时间:
var time = "3:20 PM";
var patternParsed = time.replace(new RegExp("(\\W", "g")," ").split(" ");
o / p for patternParsed是3,20,PM
但我希望为韩国时间字符串
做类似的事情当我为韩国时间字符串尝试上述正则表达式时,它不起作用
var koreanTime = new Date().toLocaleTimeString('ko-KR');
document.writeln("\nKoreanTime : "+koreanTime);
var patternParsed = koreanTime.replace(new RegExp("(\\W", "g")," ").split(" ");
document.writeln("\nparsed pattern : "+patternParsed);
有人可以帮我解决这个问题吗?我可以拆分不同语言环境的timeString吗?
答案 0 :(得分:0)
要获得hours,minutes,[AM|PM]
的标准化输出,考虑到语言环境会增加不必要的复杂性,您已经拥有了Date对象,可以使用它来生成值:
var time = new Date();
if (time.getHours() < 12) {
var patternParsed = time.getHours() + "," + time.getMinutes() + ",AM";
} else {
var patternParsed = (time.getHours() - 12) + "," + time.getMinutes() + ",PM";
}
var koreanTime = time.toLocaleTimeString('ko-KR');
document.writeln("\nKoreanTime : " + koreanTime + "\nparsed pattern : " + patternParsed);