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..
Formats a date value according to locale rules.
{{ value_expression | date [ : format [ : timezone [ : locale ] ] ] }}
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"}));
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.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.