我从数据库中获取了utc日期时间。我想通过用户时区名称显示此utc日期时间。那么如何使用javascript解决此问题
如果您有任何想法,请与我分享这个问题。

最佳答案

尝试Moment Timezone

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");

newYork.format();    // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format();     // 2014-06-01T17:00:00+01:00


演示:



var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");


$('#newYork').html(newYork.format());
$('#losAngeles').html(losAngeles.format());
$('#london').html(london.format());

<script src="http://momentjs.com/downloads/moment-with-locales.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://momentjs.com/downloads/moment-timezone-with-data.min.js"></script>

<p><strong>newYork:</strong> <span id="newYork"></span></p>
<p><strong>losAngeles:</strong> <span id="losAngeles"></span></p>
<p><strong>london:</strong> <span id="london"></span></p>

07-24 16:23