鉴于UTC时间,将其转换为小时,分钟和上午/下午的格式(例如'下午3点30分')的简单方法是什么?
答案 0 :(得分:0)
<script type="text/javascript">
<!--
var a_p = "";
var d = new Date();
var curr_hour = d.getHours();
if (curr_hour < 12)
{
a_p = "AM";
}
else
{
a_p = "PM";
}
if (curr_hour == 0)
{
curr_hour = 12;
}
if (curr_hour > 12)
{
curr_hour = curr_hour - 12;
}
var curr_min = d.getMinutes();
document.write(curr_hour + " : " + curr_min + " " + a_p);
//-->
</script>
http://www.webdevelopersnotes.com/tips/html/formatting_time_using_javascript.php
答案 1 :(得分:0)
使用以下功能,您可以更改任何格式的utc时间: -
var getDateString = function(date, format) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
getPaddedComp = function(comp) {
return ((parseInt(comp) < 10) ? ('0' + comp) : comp)
},
formattedDate = format,
o = {
"y+": date.getFullYear(), // year
"M+": months[date.getMonth()], //month
"d+": getPaddedComp(date.getDate()), //day
"h+": getPaddedComp((date.getHours() > 12) ? date.getHours() % 12 : date.getHours()), //hour
"m+": getPaddedComp(date.getMinutes()), //minute
"s+": getPaddedComp(date.getSeconds()), //second
"S+": getPaddedComp(date.getMilliseconds()), //millisecond,
"t+": (date.getHours() >= 12) ? 'PM' : 'AM'
};
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
formattedDate = formattedDate.replace(RegExp.$1, o[k]);
}
}
return formattedDate;
};
现在您可以使用它来格式化任何日期对象,如: -
getDateString(new Date(), "h:m:s:t")
o/p: "01:34:22:AM"
getDateString(new Date(), "y:M:d")
o/p:- "2014:Jun:20"
getDateString(new Date(), "y/M/d h:m:s:t")
o/p:- "2014/Jun/20 01:34:22:AM"
答案 2 :(得分:0)
// d = new Date(utc);
var hour = d.getHours() == 0 ? 12 : (d.getHours() > 12 ? d.getHours() - 12 : d.getHours());
var min = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
var ampm = d.getHours() < 12 ? 'AM' : 'PM';
var time = hour + ':' + min + ' ' + ampm;