Chrome的日期/时间格式问题 [英] Date/Time format issue with Chrome
问题描述
ChangedDate:\ / Date(1349469145000 )\ /
在FF和IE中,我得到12小时格式的上述日期(10 / 5/2012 - 3:32:25 PM)使用下面的帮助器函数:
Handlebars.registerHelper('FormatDate',函数(日期){
if(date == null)
return;
else {
var value = new Date(parseInt(date.substr(6))) ;
返回value.getMonth()+ 1 +/+ value.getDate()+/+ value.getFullYear()+ - + value.toLocaleTimeString();
}
});
不过,在Chrome中,我仍然可以使用24小时格式(10/5/2012 - 15:32 :25)。
如何在Chrome中以12小时格式获取日期/时间值?
使用 toLocaleTimeString 当意图向用户显示使用用户选择的区域格式格式化的
字符串时。请注意,根据操作系统和用户的设置,此方法由于其性质而表现不同
。
您可能更适合更改此行:
return value.getMonth()+ 1 +/ + value.getDate()+/+ value.getFullYear()+ - + value.toLocaleTimeString();
到:
return value.getMonth()+ 1 +/+ value.getDate()+/+ value.getFullYear()+ - +(value.getHours()> 12?value。 getHours() - 12:value.getHours())+:+ value.getMinutes()+:+ value.getSeconds();
我们在何处检查小时是否为> 12
,如果是的话,我们从这个数字中减去12。
(value.getHours()> 12?value.getHours() - 12:value.getHours())
所以你的例子 15:32:25
应该是 15 - 12 = 3
: 3:32:25
。
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getMinutes
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getHours
编辑
//设定前充足
var date = new Date(10/5/2012);
date.setHours(15,32,25,00);
//从日期获取数据
var month = date.getMonth()+ 1;
var day = date.getDate();
var year = date.getFullYear();
var hours = date.getHours();
var amOrPm =AM;
if(date.getHours()> 12){
hours = date.getHours() - 12;
amOrPm =PM;
}
var minutes = date.getMinutes();
if(分钟<10)
分钟=0+分钟;
var seconds = date.getSeconds();
if(seconds <10)
seconds =0+ seconds;
var dateString = month +/+ day +/+ year + - + hours +:+ minutes +:+ seconds;
console.log(dateString);
我让这个例子比需要的更详细一点,但它有助于向您展示发生了什么。希望它有帮助。
压缩下来看起来像这样:
//从日期获取数据
var dateString =(date.getMonth()+ 1)+/+ date.getDate()+/+ date.getFullYear()+ - +(date。 getHours()> 12?date.getHours() - 12:date.getHours())+:+(date.getMinutes()< 10?0+ date.getMinutes():date.getMinutes ))+:+(date.getSeconds()<10?0+ date.getSeconds():date.getSeconds())++(date.getHours()> 12?PM :AM);
I get the date/time value as below in JSON:
"ChangedDate":"\/Date(1349469145000)\/"
In FF and IE, I get the above date in 12 hr format (10/5/2012 - 3:32:25 PM) using the helper function below:
Handlebars.registerHelper('FormatDate', function (date) {
if (date == null)
return "";
else {
var value = new Date(parseInt(date.substr(6)));
return value.getMonth() + 1 + "/" + value.getDate() + "/" + value.getFullYear() + " - " + value.toLocaleTimeString();
}
});
however, in Chrome I still get the 24 hr format (10/5/2012 - 15:32:25).
How do I get the date/time value in 12 hr format in Chrome?
Use toLocaleTimeString when the intent is to display to the user a string formatted using the regional format chosen by the user. Be aware that this method, due to its nature, behaves differently depending on the operating system and on the user's settings.
You may be better off changing this line:
return value.getMonth() + 1 + "/" + value.getDate() + "/" + value.getFullYear() + " - " + value.toLocaleTimeString();
to:
return value.getMonth() + 1 + "/" + value.getDate() + "/" + value.getFullYear() + " - " + (value.getHours() > 12 ? value.getHours() - 12 : value.getHours()) + ":" + value.getMinutes() + ":" + value.getSeconds();
Where we check to see if the hours are > 12
and if so we subtract 12 from that number.
(value.getHours() > 12 ? value.getHours() - 12 : value.getHours())
So your example 15:32:25
would be 15 - 12 = 3
: 3:32:25
.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getSeconds
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getMinutes
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getHours
EDIT
//set up example
var date = new Date("10/5/2012");
date.setHours(15,32,25,00);
//Get data from date
var month = date.getMonth()+1;
var day = date.getDate();
var year = date.getFullYear();
var hours = date.getHours();
var amOrPm = "AM";
if(date.getHours() > 12){
hours = date.getHours() - 12;
amOrPm = "PM";
}
var minutes = date.getMinutes();
if(minutes < 10)
minutes = "0" + minutes;
var seconds = date.getSeconds();
if(seconds < 10)
seconds = "0" + seconds;
var dateString = month + "/" + day + "/" + year + " - " + hours + ":" + minutes + ":" + seconds;
console.log(dateString);
I made this example a bit more detailed than needed, but it helps to show you what's going on. Hope it helps.
Condensed down this would look something like:
//Get data from date
var dateString = (date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear() + " - " + (date.getHours() > 12 ? date.getHours() - 12 : date.getHours())+ ":" + (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":" + (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()) + " " + (date.getHours() > 12 ? "PM" : "AM");
这篇关于Chrome的日期/时间格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!