0

I am using Angular.js with angular-moment and I am trying to display a date in a different timezone:

p(style="font-size: 70%",am-time-ago="thing.date")

I am including moment.js, as well as the moment.js timezone data (this file). In my .html file:

  <script src="bower_components/moment/moment.js"></script>
  <script src="bower_components/moment-timezone/moment-timezone-with-data.js"></script>

I have also added the configuration in the Angular.js file, as it says on the angular-moment Github page:

angular.module('myApp')
  .constant('angularMomentConfig', { timezone: 'America/New_York' })

I have tried quite a couple of permutations up to now, but nothing seems to work. I have no idea what I might be doing wrong, so any suggestion is more than welcome.

cgf
  • 3,369
  • 7
  • 45
  • 65
  • Hmmm. "I have tried a lot [..] and nothing works" is obfruscating the most valuable thing: what is wrong with the code and what you know that is NOT the cause. Helps in guessing what is happening, when no stacktrace is given. – mico Apr 05 '15 at 18:47
  • @mico: I do agree that, in general, that would be more helpful, but I have no idea where things could go wrong. That is why I am asking for suggestions, just things that maybe I could check. My level of Angular is not that great, and I have never used moment.js before, so I have no idea how everything should fit together. I am quite sure that I have followed the docs, but I keep thinking that maybe I'm missing something. – cgf Apr 05 '15 at 18:52
  • Could you tell, what it does instead of showing the time you want? – mico Apr 05 '15 at 18:56
  • It displays a date in the future, even though the time is in the past. So for example, if the date is 19:00 and the current time is 19:10, it will say something like "in an hour" even though it should say "10 minutes ago". I am assuming that the issue has something to do with the default timezone, but I can't change it, hence this question. – cgf Apr 05 '15 at 19:43
  • Yes, my first action would be to be sure which timezone the thing.date is and after that see how it relates to new_york time zone. – mico Apr 05 '15 at 20:01
  • Please show some actual data examples. Time "ago" has little to do with time zones, but you might be *parsing* the data in a way that does. – Matt Johnson-Pint Apr 06 '15 at 15:00
  • For example, I have the date `2015-04-07T00:36:29.000Z` in my `thing.date` field which is used as shown in the post above ^. The current hour is 00:43, but moment.js outputs `is an hour`, instead of `6 minutes ago`. I do nothing special to that date, simply doing `console.log(thing.date)` outputs `2015-04-07T00:36:29.000Z`. – cgf Apr 06 '15 at 23:44

2 Answers2

1

You say to use angular-moments.js but are not including the angular-moments.js itself to your code.

Look instructions from https://www.npmjs.com/package/angular-momentjs , there the suggestion is to use bower and npm with the following commands:

You can download angular-momentjs by:

  • (preferred) Using bower and running bower install angular-momentjs --save
  • Using npm and running npm install angular-momentjs --save

and the result would be:

 <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>

 <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>

 <script src="app/bower_components/angular-momentjs/angular-momentjs.js"></script>
Community
  • 1
  • 1
mico
  • 12,730
  • 12
  • 59
  • 99
  • Sorry, the issue that I can't change the timezone, it does display a valid string though saying "10 minutes ago" or similar. – cgf Apr 05 '15 at 19:43
  • Could it relate to inconsistent daylightsaving issue like in http://stackoverflow.com/questions/17559726/moment-js-timezones-and-daylight-savings ? DayLightSaving (DST) needs to be handled separately in some casea with isDST( ) function like in http://stackoverflow.com/questions/21918095/moment-js-how-to-detect-daylight-savings-time-and-add-one-day – mico Apr 05 '15 at 21:08
  • Hmm, I assume that even if it was, it would still have to change the time shown from "in 1 hour" to, for example, "4 hours ago". The problem I have is that the "distance" to that date doesn't change no matter what timezone I attach to it. – cgf Apr 05 '15 at 22:03
1

Here is my solution. Wandered to this question trying to find solution. So Here it is if someone else wanders here too looking for same answer.

Since version 1.0.0-beta.1, time zone can be set using amMoment service. Just inject the amMoment service and call changeTimeZone:

amMoment.changeTimezone('Europe/London');
Firze
  • 3,939
  • 6
  • 48
  • 61