I have a function that converts a SQL datetime stamp into a formatted time. It looks good on an iOS device, but its displayed as military time on an android device. How can I get this to return the toLocaleTimeString() as not military time on an android device?
function fromDateString(str) {
var res = str.match(/\/Date\((\d+)(?:([+-])(\d\d)(\d\d))?\)\//);
if (res == null)
return new Date(NaN); // or something that indicates it was not a DateString
var time = parseInt(res[1], 10);
if (res[2] && res[3] && res[4]) {
var dir = res[2] == "+" ? -1 : 1,
h = parseInt(res[3], 10),
m = parseInt(res[4], 10);
time += dir * (h*60+m) * 60000;
}
return formatdate.toLocaleTimeString();
}
The
Date.toLocaleTimeString()function` is “implementation dependent” which means that if you want to guarantee a certain format on all devices then you must apply it yourself.Here’s how I would do it: