7

On server side DateTime is saved as per UTC (2016-03-24 17:45:12) and to client it is always returned DateTime as UTC. There are different users from world.

What is the best way to do date-time display management with i18n and l10n in angular? Also i want that it should display date-time as per timezone of the user.

There are lots of feeds shown from all over the world like StackOverflow Posts. From server side always DateTime are passed in UTC time zone. On client side, date time can be displayed in two formats like below.

  1. Showing date time of post as per time zone
  2. Showing time-ago facility like (Just Now, Minute Ago, Hour Ago, Day Ago, Month Ago, Year Ago, 2 Years ago, 3 Years ago, etc)

This type of date times displayed across system multiple times for posts. So what is best way to use angular to have very less code for managing this.

How/When/Why to use Filters, Directives, Expressions for this?

(P.S. I am new to angular.)

Update: More explanation:

Post object can be single page or array of objects. And post date time will be like UTC: (2016-03-24 17:45:12)

So there are two cases which can be displayed like:

  1. 5 Months Ago
  2. 2016-03-24 11:15 PM (IST time)
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • 2
    It's unclear exactly what you are asking. Is this about what the best practices are? Do you have a specific question on how to do something? If you're looking for a way to achieve the scenarios you described, consider [moment.js](http://momentjs.com). There is a good angular integration [here](https://github.com/urish/angular-moment) – Matt Johnson-Pint Aug 27 '16 at 21:45
  • @MattJohnson: Yeah, I was looking for similar solution like that. I have updated question. – Somnath Muluk Aug 28 '16 at 07:04
  • I'm not sure how to answer this other than to say, "use a filter" - of which you've already identified the Angular date filter, and I gave you a link to using moment.js with angular-moment filters. There's not much more to say, since your question is not very specific. I would vote to close as "too broad" except the open bounty prevents that. – Matt Johnson-Pint Aug 29 '16 at 03:54
  • @MattJohnson: I will see how to use angular-moment filter. I have updated question like there are only two use cases with example. I understand now there is need to use 2 filters. 1st for showing `Ago` Style time. 2nd for time as per `client browser's timezone`. I think this question is not that broad. – Somnath Muluk Aug 29 '16 at 05:52
  • You don't seem to be *asking* anything at all. Are you wanting someone to create the code for you? What's wrong with the sample code in the docs? Sorry, I'd like to help, but I'm not sure what more you need. – Matt Johnson-Pint Aug 29 '16 at 20:27
  • @MattJohnson Neither moment.js nor moment-timezone.js _detects_ timezone afaik. I think that's what OP is asking. http://stackoverflow.com/questions/6939685/get-client-time-zone-from-browser – adamdport Aug 30 '16 at 16:30
  • 1
    @adamdport - Actually, moment-timezone can do that with `moment.tz.guess()`. But I didn't get that at all from the OP. – Matt Johnson-Pint Aug 30 '16 at 19:07
  • @MattJohnson: Yes. I was asking as per Client's TimeZone. – Somnath Muluk Aug 31 '16 at 03:15
  • @SomnathMuluk - No, the `guess` API is for returning the string that is the IANA identifier of the time zone, such as `America/Los_Angeles` for US Pacific Time. It is not needed in order to work within the client's local time, just to *identify* the client's local time zone. – Matt Johnson-Pint Aug 31 '16 at 03:45

1 Answers1

0

You can trust the users' browsers. To display a date, you can use myDate.toLocaleString(). It will display the date using user's locale settings.

For "time-ago" format, you can use AngularJS version of moment.js: https://github.com/urish/angular-moment#am-time-ago-directive

yogurt
  • 380
  • 1
  • 10