当javascript中的系统首选项和用户首选项不同时,时区转换问题

时间:2016-11-10 03:11:09

标签: javascript jquery momentjs

当我的系统偏好Asia / Kolkata,用户偏好输入日期是America / Los_Angeles两者不同时,

日期不正确

当我的系统偏好&用户偏好相同

jsfiddle示例:http://jsfiddle.net/uq99udc9/728/

//system preference Asia/Kolkata
// input date America/Los_Angeles --- 2016-11-09 02:00:00
var date = new Date("2016-11-09 02:00:00");
var gmtdate = date.toGMTString(); -- converting into GMT
$("#divThai").text(moment.tz(gmtdate,'Asia/Kolkata'));
$("#divUsa").text(moment.tz(gmtdate,'America/Los_Angeles'));

任何人都可以帮助我解决这个问题。

先谢谢,

1 个答案:

答案 0 :(得分:0)

试试这个:



// Parse the date in Los Angeles timezone.
const date = moment.tz('2016-11-09 02:00:00', 'America/Los_Angeles');
// Output the date.
console.log(date.toString());
// Output the date in Asia/Kolkata timezone.
console.log(date.tz('Asia/Kolkata').toString());

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.9/moment-timezone-with-data.min.js"></script>
&#13;
&#13;
&#13;