[英]How to convert a UTC string into a momentJS in visitor's local timezone?
我發現了這個小提琴: https : //jsfiddle.net/FLhpq/4/
UTC<br/>
<div id="divUTC"></div><br/>
Your Local Time with respect to above UTC time<br/>
<div id="divLocal">
</div>
$(function(){
setInterval(function(){
var divUtc = $('#divUTC');
var divLocal = $('#divLocal');
//put UTC time into divUTC
divUtc.text(moment.utc().format('YYYY-MM-DD HH:mm:ss'));
//get text from divUTC and conver to local timezone
var localTime = moment.utc(divUtc.text()).toDate();
localTime = moment(localTime).format('YYYY-MM-DD HH:mm:ss');
divLocal.text(localTime);
},1000);
});
我希望這正是您所需要的-字符串中的utc時間轉換為本地化時間。
有一種更簡單的方法可以做到這一點。
如果您知道自己有一個UTC日期,並且希望將其轉換為用戶的本地時間,則只需執行以下操作:
moment.utc("2014-11-06 00:00:00").local()
這樣就產生了用戶本地時間的片刻。 此時的.format()
結果將以用戶的本地時間為准。
不要看_d
的值。 這是不准確的。 請參閱以下內容: http : //momentjs.com/guides/#/lib-concepts/internal-properties/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.