Angular JS - Date changes when submitting to $http - Timezone issue

前端 未结 1 1589
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 08:41

I am having a strange problem where a Date changes when it is passed to API through $http.put, I suspect a timezone issue:

Datepicker triggers ng-change eve

相关标签:
1条回答
  • 2021-02-07 09:23

    I think it is a TZ issue, b/c the difference between your GMT+0100 and your StartDate=2014-06-09T23:00:00.000Z is 5 hours.

    .

    The issue:

    Your local time is currently BST (British Summer Time) equivalent to GMT +1

    This is going to be the default time when you make your API call.

    However, the API was written by Google in California, and they are rather egocentric. Therefore, they're automatically converting the time since you're not providing any date formatting "instructions".

    In other words, Chrome does the conversion for you nicely when you print to the console.log(), but when you make your $http.put, Angular, without explicit formatting for you, makes the call using it's default TZ (which is PST)

    .

    Resolution

    You need to force the date formatting to your locale.

    • Angular template

      {{ date_expression | date : 'yyyy-MM-dd HH:mm:ss'}}
      
    • In JavaScript

      $filter('date')(date, 'yyyy-MM-dd HH:mm:ss')
      
    • using localization

      <html>
        <head>
          <title>My Angular page</title>
          <script src="angular-locale_en-uk.js"></script>
          ...
        </head>
        <body>
          ...
        </body>
      </html>
      
    0 讨论(0)
提交回复
热议问题