javascript - 将字符串验证为 JavaScript 日期格式
问题描述
我在验证来自new Date()
Object 的以下字符串时遇到问题。
"2020-10-06T14:23:43.964Z"
. 就我而言,我期望这种输入格式或new Date()
. 我正在尝试写一些类似于
const isValidDate = (d: Date):boolean => {
if (d instanceof Date || /* check if valid Date string here */) {
return true
}
return false
}
解决方案
也许这可以帮助你。
Date.prototype.isValid = function () {
// If the date object is invalid it
// will return 'NaN' on getTime()
// and NaN is never equal to itself.
return this.getTime() === this.getTime();
};
推荐阅读
- c - 文件的并发读写
- gpu - NVIDIA OpenCL 设备版本
- javascript - all:revert in css 取消羽化图标
- laravel - Laravel:遵循模型的第二个关系
- javascript - 在 Vue.js 2 中将 v-model 链接到对象的属性
- python - 在python中加速groupby
- javascript - 类方法的打字稿联合从不具有参数类型
- flutter - 在颤动中找到现有用户时如何更改文本字段错误标签?
- ruby-on-rails - Rails 路由:如何重命名(嵌套)资源块中的 params-Hash 键?
- windows - 如何在 Windows 批处理文件中传递密码和 ssh 命令