Deepa
Deepa

Reputation: 970

Convert UTC time to local time in angular

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..

Upvotes: 2

Views: 15537

Answers (6)

Pavlo Kozachuk
Pavlo Kozachuk

Reputation: 283

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")}}

Upvotes: 2

Sunny Parekh
Sunny Parekh

Reputation: 993

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.

Upvotes: 0

Aditya Bhave
Aditya Bhave

Reputation: 1028

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>

Upvotes: 0

wentjun
wentjun

Reputation: 42596

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"}));

Upvotes: 2

Umbro
Umbro

Reputation: 2204

Formats a date value according to locale rules.

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

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

Upvotes: 2

FunkeyFlo
FunkeyFlo

Reputation: 295

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

Upvotes: 0

Related Questions