我正在尝试对从 Nodejs 中的帖子接收的参数进行一些验证。 body 是这样的:
{
"startDateTime": "2019-10-01 00:00:01",
"endDateTime": "2019-10-01 23:59:59",
"interval": "00:00:01"
}
所以,我需要验证 startDateTime
和 endDateTime
是否处于模式 "yyyy-MM-dd hh:mm:ss"
,以及“hh:mm:ss”中的
间隔
我尝试使用 moment 包,但没有成功
const testDate = moment(startDateTime, "yyyy-MM-dd hh:mm:ss", true).isValid()
我正在传递 StartDateTime = "2019-10-01 00:00:01"
且 testDate
被分配为 false。
有人可以帮忙吗?
最佳答案
您使用了错误的格式。请尝试使用“YYYY-MM-DD HH:mm:ss”
。
To learn more about Moment.js Formats, take a look at its documentation.
let response = {
"startDateTime": "2019-10-01 00:00:01",
"endDateTime": "2019-10-01 23:59:59",
"interval": "00:00:01"
};
const testDate = moment(response.startDateTime, "YYYY-MM-DD HH:mm:ss", true).isValid();
console.log(testDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
关于javascript - 验证包含特定模式中的日期时间的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58591132/