2016-02-26 106 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);

相关问题