2017-04-24 27 views
0

我有一些日曆文本框的小時和分鐘,用於在我的角度應用程序中捕獲日期時間。從前端到web api的日期發佈問題

這被轉換爲完整的日期如下圖所示:如果我張貼此notificationDate在網頁API輸入

accidentVM.AccidentNotificationDateFull = new Date(accidentVM.AccidentNotificationDate.getFullYear(), accidentVM.AccidentNotificationDate.getMonth(), accidentVM.AccidentNotificationDate.getDate(), 
               parseInt(accidentVM.AccidentNotificationHour), parseInt(accidentVM.AccidentNotificationMinute), 0); 
var notificationDate = accidentVM.AccidentNotificationDateFull; 

現在和它變得不同,當達到網絡API,因爲JavaScript是追加我的本地時區(其是+ 5:30)在我的情況。

爲了避免這種

我的確從客戶side.This在GMT一個調整基本上注意到GMT偏移,然後減去或使之成爲當它到達服務器再次正確的從輸入添加。

功能看起來像

this.convertDate = function (dateVM) {    
       var dateObj = dateVM; 
       if (new Date().getTimezoneOffset() < 0) { 
        dateVM = new Date(dateObj.setTime(dateObj.getTime() - new Date().getTimezoneOffset() * 60 * 1000)); 
       } 
       else { 
        dateVM = new Date(dateObj.setTime(dateObj.getTime() + new Date().getTimezoneOffset() * 60 * 1000)); 
       }     
}; 

這解決了這個問題,我現在看到越來越貼出正確的日期時間,但它仍然在最後追加爲T,而在服務器達到。

見輸入下面是從前端 enter image description here

,當它到達的Web API變得

enter image description here

和相應的提琴手要求

enter image description here

如果您已經注意到它正在變成UTC或在最後附加Z.

我的任務是,如果有什麼辦法可以擺脫Z或更具體的DateTime種類應該是未指定/本地?

+0

ISO 8601是傳輸日期爲字符串的最佳格式。您的後端應該簡單地讀取該字符串並將其解析爲瞬間。然後,您可以應用您想要的任何時區 – Phil

回答

0

我在WebApiConfig類中添加了以下本地日期格式設置。這將告訴web api json格式化程序轉換爲本地時區。

var jsonFormatter = config.Formatters.JsonFormatter; 
jsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; 

因此,我不需要明確的客戶端操縱格林威治標準時間。