简体   繁体   English

将日期字符串转换为日期 object

[英]Convert date string to a date object

Getting the date from the user and storing the value as a string variable.从用户那里获取日期并将值存储为字符串变量。 The value looks like this Fri Aug 27 2021 00:00:00 GMT+0530 (India Standard Time)该值看起来像这样Fri Aug 27 2021 00:00:00 GMT+0530(印度标准时间)

I want to convert that string again into a new Date() object for further configurations (I need to add the number of days to the selected date and show it on another Angular mat-datepicker) How can I do this..?我想将该字符串再次转换为新的 Date() object 以进行进一步配置(我需要将天数添加到所选日期并将其显示在另一个 Angular mat-datepicker 上)我该怎么做..?

You can cast a date string directly to a Date object like:您可以将日期字符串直接转换为日期 object,例如:

let myDate = new Date("Fri Aug 27 2021 00:00:00 GMT+0530 (India Standard Time)");

You can manipulate your date as follows:您可以按如下方式操作您的日期:

// For instance adding a day:
let today = new Date("Fri Aug 27 2021 00:00:00 GMT+0530 (India Standard Time)");

let tomorrow = new Date()
tomorrow.setDate(today.getDate() + 1);

You can achieve this by using momentjs你可以通过使用momentjs来实现

let momentObject = moment('Fri Aug 27 2021 00:00:00 GMT+0530 (India Standard Time)')
momentObject.add(1,'days');

Then to finally get your date object然后终于得到你的约会对象 object

let dateToBeSet = momentObject.toDate();

then you can use this dateToBeSet object to do whatever you want然后你可以使用这个 dateToBeSet object 做任何你想做的事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM