Đây là tốt nhất mà tôi đã nhìn thấy cho xa mà có thể sử dụng múi giờ máy tính để bàn của khách hàng và thay đổi theo thời gian thực với các thiết lập múi giờ:
<script type="text/javascript">
//Use: timeZoneConvert("2015-11-03T17:36:20.970");
//Mon Nov 02 2015 17:36:20 GMT-0600 (Central Standard Time) [Date object]
//To format string use: timeZoneConvertFormatted("2015-11-03T17:36:20.970")
//November 2, 2015 5:36 PM
//Works even when I change client timezone to Pacific Standard
//Mon Nov 02 2015 15:36:20 GMT-0800 (Pacific Standard Time)
var months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function timeZoneConvert(dateStr) {
var d = parse_iso8601(dateStr);
//Change (- 360) constant for your server, mine is Central Standard
var dTimezoneOffset = new Date(d.getTime() - ((new Date()).getTimezoneOffset() - 360)*60000);
return dTimezoneOffset;
}
function timeZoneConvertFormatted(dateStr) {
return fDateTime(timeZoneConvert(dateStr));
}
function fDateTime(date1) {
var date = date1.getDate();
var year = date1.getFullYear();
var month = months[date1.getMonth() + 1];
var h = date1.getHours();
var m = date1.getMinutes();
var ampm = "AM";
if(m < 10) {
m = "0" + m;
}
if(h > 12) {
h = h - 12;
var ampm = "PM";
}
return month + " " + date + ", " + year + " " + h + ":" + m + " " + ampm;
}
var iso8601extended = /^\d{4}(-\d{2}(-\d{2}([T ]\d{2}(:\d{2}(:\d{2})?)?([,.]\d+)?(Z|[+-]\d{2}(:\d{2})?)?)?)?)?$/;
var iso8601basic = new RegExp(iso8601extended.source.replace(/[:-]\\d/g, '\\d'));
var firstNumber = /[^\d]*(\d+)/g;
function parse_iso8601(s) {
s = s.replace(/,/g, '.');
var matches = iso8601extended.exec(s);
if (matches) {
s = s.substr(0, 10).replace(/-/g, '') + s.substr(10).replace(/:/g, '');
}
matches = iso8601basic.exec(s);
if (!matches) {
return null;
}
var d = new Date();
d.setUTCFullYear(toNumber(matches[0].substring(0, 4)));
d.setUTCMonth(matches[1] ? toNumber(matches[1].substr(0, 2)) - 1 : 0);
d.setUTCDate(matches[2] ? toNumber(matches[2].substr(0, 2)) : 1);
var hours = 0, minutes = 0, seconds = 0, milliseconds = 0;
var fraction = matches[6] ? parseFloat(matches[6]) : 0;
if (matches[3]) {
hours = toNumber(matches[3].substr(1, 2));
if (matches[4]) {
minutes = toNumber(matches[4].substr(0, 2));
if (matches[5]) {
seconds = toNumber(matches[5].substr(0, 2));
milliseconds = 1000 * fraction;
} else {
seconds = 60 * fraction;
}
} else {
minutes = 60 * fraction;
}
}
if (!matches[7]) {
d.setHours(hours);
d.setMinutes(minutes);
} else {
d.setUTCHours(hours);
d.setUTCMinutes(minutes);
}
d.setUTCSeconds(seconds);
d.setUTCMilliseconds(milliseconds);
if (matches[7] && matches[7] != 'Z') {
offset = toNumber(matches[7].substr(1, 2)) * 60;
if (matches[8]) {
offset += toNumber(matches[8].substr(0, 2));
}
d.setTime(d.getTime() + 60000 * offset * (matches[7].substr(0, 1) == '-' ? 1 : -1));
}
return d;
}
function toNumber(s) {
return parseInt(s.replace(/^0+(\d)/, '$1'));
}
</script>
Nguồn
2015-11-04 00:22:27
thể dup http://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to- time-in-javascript – elclanrs
Dấu thời gian unix và dấu thời gian iso không giống nhau, vì vậy không, nó không phải là một bản sao. – Ian
Dấu thời gian ISO của bạn có chuỗi không? và bạn muốn phân tích nó thành lớp javascript Date() '? – jfriend00