Date/Time format issue with Chrome

你说的曾经没有我的故事 提交于 2020-01-06 06:52:35

问题


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?


回答1:


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.

EXAMPLE

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");

EXAMPLE



来源:https://stackoverflow.com/questions/12784337/date-time-format-issue-with-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!