myDate = "2021-09-13T00:00:00">
现在我尝试从字符串
中创建一个日期new Date().setDate(new Date(options.startDate).getDate())
即"2021-09-13T17:56:08-05:00"
这里的问题,不知何故,它使用我的当前时间与日期,但我希望能够将时间设置为午夜或中午。
我在Stackoverflow上看了很多帖子,但是没有解决方案适合我。
有什么意见吗?
如果你想解析一个日期,然后改变小时(时间),那么你可以使用setHours。这假定您希望保持本地时间。
要显式覆盖到精确的时间,可以使用重载函数:
setHours(hoursValue)
setHours(hoursValue, minutesValue)
setHours(hoursValue, minutesValue, secondsValue)
setHours(hoursValue, minutesValue, secondsValue, msValue) <-- this
片段:
const myDate = "2021-09-13T00:00:00";
const parsedDate = new Date(myDate);
console.info(parsedDate.toString());
// set to 12pm
parsedDate.setHours(12, 00, 00, 00);
console.info(parsedDate.toString());
// set to midnight, which is essential next day
parsedDate.setHours(24, 00, 00, 00);
console.info(parsedDate.toString());