21

I have rest service which returns a collection of objects and one of the field of the item is a date string (ISO-8601 format ) and the date value as follows

"createdDate" : "2017-02-21T12:56:50.907",

In the angular4 UI I put DatePipe to format the above date

{{resultItem.createdDate| date:'short'}}

and I am getting wrong conversion as follows

2/21/2017, 7:56 AM

instead of

2/21/2017, 0:56 AM

Vega
  • 27,856
  • 27
  • 95
  • 103
Jo Paul
  • 783
  • 1
  • 9
  • 14
  • 2
    It's probably due to timezones, `createdDate` is being parsed as UTC rather than your local time, so it gets converted from UTC to your local time. – Adam Apr 06 '17 at 20:27

6 Answers6

32

I resolved the issue by adding a custom pipe.

My custom pipe is based on the solution provided by Birwin. Thanks Birwin.

Here is my custom pipe named UtcDate

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'utcDate'
})
export class UtcDatePipe implements PipeTransform {

  transform(value: string): any {

    if (!value) {
      return '';
    }

    const dateValue = new Date(value);

    const dateWithNoTimezone = new Date(
      dateValue.getUTCFullYear(),
      dateValue.getUTCMonth(),
      dateValue.getUTCDate(),
      dateValue.getUTCHours(),
      dateValue.getUTCMinutes(),
      dateValue.getUTCSeconds()
    );

    return dateWithNoTimezone;
  }
}

And I also used default date pipe to format

{{createdDate | utcDate | date:'short'}}
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
Jo Paul
  • 783
  • 1
  • 9
  • 14
27

You can pass another param to date pipe as follows:

{{resultItem.createdDate | date : 'short' : 'UTC'}}

This param can be a timezone like '-0430' or just 'GMT'

See documentation: https://docs.angularjs.org/api/ng/filter/date

Samuel Luis
  • 279
  • 3
  • 2
15

You may need to create a UTC date from your date with timezone... I assume you are in the pacific timezone as the time is 7 hours from UTC...

Try this code to get a new date object without Timezone (assuming your variable is named "date"):

var datewithouttimezone = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),  date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
birwin
  • 2,524
  • 2
  • 21
  • 41
  • 1
    Thanks Birwin. if it is single date I can easily do like you mentioned above . My REST service returning a collection of object and this date is one of the field in that. is there any way I can apply this logic to entire collection ? – Jo Paul Apr 07 '17 at 13:47
  • 1
    As noted in other comments, now that angular 5+ is released, there are other options. – birwin Sep 05 '19 at 13:22
0

I used moment.js in this scenario. it worked for me. The angular version is 8

import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';

@Pipe({
  name: 'utcDate'
})
export class UtcDatePipe implements PipeTransform {

  transform(value: string): any {

    if (!value) {
      return '';
    }

    const dateWithNoTimezone = new Date(moment.utc(value).format());

    return dateWithNoTimezone;
  }
}

HTML:

<small class="text-muted ml-auto">{{n.createdAt | utcDate | date :'dd/MM/yyyy h:mm a '}}</small>
Kumaresan Perumal
  • 1,926
  • 2
  • 29
  • 35
0

use these :

in your component:

import * as moment from 'moment'; 

in html page

{{resultItem.createdDate | date : 'MM/dd/yyyy HH:mm': 'UTC' }}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Mamad Dansoko
  • 561
  • 4
  • 3
-1

Add the below code to html file,

{{ value | date:'short':'UTC+offset'}}

In component.ts file get the value of offset,

this.offset = (new Date().getTimezoneOffset()); 

It will convert UTC time to local time.

Niranjana V S
  • 147
  • 2
  • 3