2016-02-26 100 views
1

我在Angular中寫了一個應用程序,我正在使用Angular UI。我有一個日期選擇器,看起來像這樣:Angular UI日期選擇器出錯日期

<input type="text" 
     ng-required="true" 
     name="targetDate" 
     uib-datepicker-popup="MMMM yyyy" 
     min-mode="'month'" 
     datepicker-mode="'month'" 
     is-open="opened" 
     ng-click="openCalendar()" 
     close-on-date-selection="true" 
     show-button-bar="false" 
     min-date="today" 
     ng-model="targetDate" /> 

的問題是,當我選擇,例如,2016年7月,我在我的targetDate模型中的價值是「2016-06-30T21:00:00.000 Z「,這恰好是2016年7月之前的3小時。我認爲這與我的當地時間和UTC時間有關,因爲我住在當地時間爲UTC +2小時的地區,但我不知道爲什麼發生這種情況以及如何解決它。有任何想法嗎?

+1

您需要使用MomentJs庫http://momentjs.com/docs/將UTC轉換爲本地時間。如果你已經知道的時候閱讀這個快速http://www.digitoffee.com/programming/get-local-time-utc-using-moment-js/94/ – Prasad

+0

http://momentjs.com/docs/ for從開始使用 – Prasad

+0

這個問題之前已經提到過:http://stackoverflow.com/questions/22623872/angular-ui-datepicker-adjusting-for-timezone – Akis

回答

0

你可以參照這個線程 - GitHub

試圖改變你的機器時區的日期OBJ默認選擇你的機器的本地時間。

或者嘗試通過getTime()解析Date對象以毫秒爲單位,如果它的值和添加10800000毫秒,這是3小時到日期模型。

因此,您的新日期模型將是您選擇的東西(以毫秒爲單位)+ 10800000 =期望的UTC日期。

+3

你真的在暗示用戶只是因爲應用程序想要以UTC時間顯示某些內容而更改其當地時間? –

+0

對於臨時解決方案,將日期對象模型轉換爲毫秒並添加10800000(3小時) – Vinay

+0

因此,您的新日期模型將是您選擇的項目(以毫秒爲單位)+ 10800000 =期望的UTC日期。 – Vinay

0

這實際上解決了我的問題,這裏是我的代碼:

service.ConvertDateToJSONDate = function (dateInput) { 
     if (dateInput === null || dateInput === undefined) { 
      return null; 
     } 
     //return "\/Date(" + dateInput.getTime().toString() + ")\/"; 
     return "\/Date(" + (dateInput.getTime() + 10800000).toString() + ")\/"; 
    }; 

用法:service.ConvertDateToJSONDate(SelectedDateObjectToBeConverted);

相關問題