Вопрос

I'm not quite sure how to set the timezone in angularjs for the date filter, which converts 1970-01-01 to 1969-12-31. I've seen this post, which explains the same problem in Java, but not sure how to approach it in AngularJs - any clues?

Это было полезно?

Решение

The JavaScript date is based on a time value that is milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The JavaScript Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.

source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

JavaScript Date type does not have timezone support* you need to use either ISO 8601 format or send timestamp in UTC.

You can also use a library like timezone-js or moment.js

Другие советы

The angular developer guide says this about timezones:

The Angular datetime filter uses the time zone settings of the browser. The same application will show different time information depending on the time zone settings of the computer that the application is running on. Neither JavaScript nor Angular currently supports displaying the date with a timezone specified by the developer.

You can make your own filter to use a specific timezone. Here is a simple one using moment.js to parse 19700101 with the GMT timezone:

app.filter('GMT', function() {
  return function(input) {
    return moment(input + ' +0000', "YYYYMMDD").format('MMM DD, YYYY');
  };
});

Here is a demo: http://plnkr.co/l5zd9Z1tli2NLmgPTWhJ

Thanks everyone for participating. After quite a bit of time trying to find an easy way to resolve the problem I've found the solution - by simply using Date and one of it's methods:

new Date(date).toISOString();

That does the job and is super short :)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top