2017-09-06 34 views
2

我試圖從用戶那裏獲取航班的日期,而且datepicker總是將日期設置爲用戶時區的日期時間對象,從而導致發送給服務器的日期錯誤。 嘗試使用ng-model-options="{ timezone: 'utc' }"角度物料日期選擇器時區忽略?

但是然後用戶在選擇日期(日期顯示爲用戶選擇之前一天顯示的日期)後看到錯誤的日期。

我如何告訴datepicker完全忽略時區 - 如果不可能我可以在FE/BE上做些什麼來將此日期轉換爲非時區日期?

我知道我可以將它轉換爲FE上的Posix,但是一旦我做到了,日期選擇器尖叫它需要日期對象。

在此先感謝...

+0

看看這個庫: https://momentjs.com/它用於管理JS中的日期(您可以在FE中使用,也可以在節點中使用BE),查看api文檔,可以轉換時區和任何您想要的內容,我希望這會對您有所幫助 – Kalamarico

回答

0

我有完全相同的問題,我最後我們使用此代碼

Date.prototype.toJSON = function() { 
    var timezoneOffsetInHours = -(this.getTimezoneOffset()/60); //UTC minus local time 
    var sign = timezoneOffsetInHours >= 0 ? '+' : '-'; 
    var leadingZero = (timezoneOffsetInHours < 10) ? '0' : ''; 

    //It's a bit unfortunate that we need to construct a new Date instance 
    //(we don't want _this_ Date instance to be modified) 
    var correctedDate = new Date(this.getFullYear(), this.getMonth(), 
    this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), 
    this.getMilliseconds()); 
    correctedDate.setHours(this.getHours() + timezoneOffsetInHours); 
    var iso = correctedDate.toISOString().replace('Z', ''); 

    return iso + sign + leadingZero + Math.abs(timezoneOffsetInHours).toString() + ':00'; 
} 

其從該其他SO質疑How to JSON stringify a javascript Date and preserve timezone

相關問題