2

I want to convert this utc format date to india date and time format in angular

2019-02-18T17:31:19-05:00

I expect in this format DD/MM/YYYY HH:MM(eg: 02/19/2019 04:01 AM). Can anyone please suggest me how to do it..

Deepa
  • 850
  • 1
  • 10
  • 11

6 Answers6

2

Formats a date value according to locale rules.

{{ value_expression | date [ : format [ : timezone [ : locale ] ] ] }}

Sourxe: https://angular.io/api/common/DatePipe

Umbro
  • 1,984
  • 12
  • 40
  • 99
2

One way to do it in vanilla JavaScript would be to make use of Date.toLocaleString(), and to convert it to the Indian timezone by setting it as the timeZone property.

new Date('2019-02-18T17:31:19-05:00').toLocaleString("en-US", {timeZone: "Asia/Kolkata"});

console.log(new Date('2019-02-18T17:31:19-05:00').toLocaleString("en-US", {timeZone: "Asia/Kolkata"}));
wentjun
  • 40,384
  • 10
  • 95
  • 107
2

in ts

import {utc} from 'moment';
utc = utc;

in html

{{ utc("2019-02-18T17:31:19-05:00").local().format("DD/MM/YYYY HH:MM")}}
Pavlo Kozachuk
  • 244
  • 3
  • 7
0

if you use a locale_id in your module all date pipes should adhere to that locale https://angular.io/api/core/LOCALE_ID

FunkeyFlo
  • 295
  • 1
  • 6
0

UTC dates should have 'Z' appended to it. E.g. 2019-02-18T17:31:00Z.

Secondly, convert the date string to a Date object by using Date.parse()

Then use date filter to display the date <span> {{ dateStr | date }}</span>

Check below JS -

angular.module('plunker', []).controller('MainCtrl', function($scope) {
    $scope.dateStr = Date.parse('2019-02-18T17:31:00Z');
});

HTML -

<div ng-controller="MainCtrl">
  {{ dateStr | date }}
</div>
Aditya Bhave
  • 998
  • 1
  • 5
  • 10
0

If you want you can use a awesome library for date and time related manipulation.

https://momentjs.com/timezone/docs/

In your case you can try out the following

moment.tz('2019-02-18T17:31:19-05:00', 'Asia/Kolkata').format('MM/DD/YYYY HH:mm a');

This will return you the date in the desired format. Let me know if any further explanation is required for this.

Sunny Parekh
  • 945
  • 7
  • 17