Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date to eastern time zone with Angular

Tags:

angularjs

I'm getting data back from the server like this:

'2015-03-05T16:51:56+00:00'

Using Angular, I'd like to display this date/time as an Eastern Time date. Is there a way to specify a different timezone with Angular? I'm doing something like:

{{ myDate | date: 'medium' }}

Which gives back:

Mar 5, 2015 11:51:56 AM

But I'd like it to display as:

Mar 5, 2015 4:51:56 PM
like image 673
cusejuice Avatar asked Mar 16 '23 20:03

cusejuice


2 Answers

From the angularJS dateFilter docs

timezone (optional) string

Timezone to be used for formatting. It understands UTC/GMT and the continental US time zone abbreviations, but for general use, use a time zone offset, for example, '+0430' (4 hours, 30 minutes east of the Greenwich meridian) If not specified, the timezone of the browser will be used.

So try:

{{ myDate | date: 'medium' : -0500 }}
like image 140
Ed_ Avatar answered Mar 28 '23 05:03

Ed_


The syntax for the date filter in HTML template binding since 1.3 allows for an optional timezone field:

{{ date_expression | date : format : timezone}}

However, 1.3 only supports the UTC timeszone.

Timezone to be used for formatting. Right now, only 'UTC' is supported. If not specified, the timezone of the browser will be used

In the 1.4 beta, it now supports more than just UTC:

Timezone to be used for formatting. It understands UTC/GMT and the continental US time zone abbreviations, but for general use, use a time zone offset, for example, '+0430' (4 hours, 30 minutes east of the Greenwich meridian) If not specified, the timezone of the browser will be used.

You could specify the timezone or the offset to use:

{{ myDate | date: 'medium' | timezone: '+0430'}}
like image 23
mkobit Avatar answered Mar 28 '23 07:03

mkobit