[英]How to convert a string to a date in angular
How to covert a string to a date in Angular13?如何将字符串转换为Angular13中的日期?
For example: str = '2022-06-09T22:00:00Z';
例如:
str = '2022-06-09T22:00:00Z';
to a datetime mm/dd/yyyy --:-- --
到日期时间
mm/dd/yyyy --:-- --
If you are okay with using a library, I would recommend to use moment.js.如果您可以使用库,我建议您使用 moment.js。 Here's a link !
这是一个链接!
Firstly, since we can not access the inbuilt functions from a string, we need to convert it to a date:首先,由于我们不能从字符串访问内置函数,我们需要将其转换为日期:
var date = new Date("2022-06-09T22:00:00Z")
Using moment.js :使用moment.js :
moment.utc(d).format('MM/DD/YYYY HH:mm:ss') // -> 06/09/2022 22:00:00
Using standard in-built functions :使用标准的内置函数:
var formatted = ("0"+(d.getUTCMonth()+1)).slice(-2) + "/" + ("0" + d.getUTCDate()).slice(-2) + "/" + d.getUTCFullYear() + " " + ("0" + d.getUTCHours()).slice(-2) + ":" + ("0" + d.getUTCMinutes()).slice(-2) + ":" + ("0" + d.getUTCSeconds()).slice(-2);
If you don't want the time in UTC, you can simply puzzle it around to remove it.如果您不希望使用 UTC 时间,您可以简单地对其进行混淆以将其删除。
new Date("2022-06-09T22:00:00Z")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.