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
6 Answers
Reset to default 2One 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
版权声明:本文标题:javascript - Convert UTC time to local time in angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741627171a2389150.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论