admin管理员组

文章数量:1295860

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

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

Share Improve this question asked Jun 20, 2019 at 10:16 Deepa Deepa 9701 gold badge10 silver badges11 bronze badges 3
  • 1 Is this to be shown on your ponent.html? Or ponent.ts? – wentjun Commented Jun 20, 2019 at 10:18
  • 1 check angular.io/api/mon/DatePipe – Ntwobike Commented Jun 20, 2019 at 10:19
  • Possible duplicate of Convert UTC date time to local date time – Cpt Kitkat Commented Jun 20, 2019 at 10:52
Add a ment  | 

6 Answers 6

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

Formats a date value according to locale rules.

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

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

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

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

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>

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

https://momentjs./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.

本文标签: javascriptConvert UTC time to local time in angularStack Overflow