javascript - MomentJS 返回意外结果
问题描述
不确定我的日期和 moment.js 做错了什么。
我有以下代码:
function(value) {
const startDate = moment(this.parent.startDate).format("DD/MM/YYYY")
const endDate = moment(value).format("DD/MM/YYYY")
console.log("SE",startDate,endDate)
return moment(startDate).isSameOrBefore(moment(endDate))
}
我的 console.log 的输出startDate
和endDate
是:
SE 15/08/2021 19/08/2021
但是由于某种原因,当调用这个函数时,它是说我的:
End date must be greater than or equal to start date
根据我的return moment(startDate).isSameOrBefore(moment(endDate))
,不应该是结束日期19/08/2021
在开始日期之后的情况15/08/2021
我错过了什么?
解决方案
比较原始日期,而不是格式化日期,因为 moment.js 不知道它们的DD/MM/YYYY
格式。
function(value) {
const startDate = moment(this.parent.startDate).format("DD/MM/YYYY")
const endDate = moment(value).format("DD/MM/YYYY")
console.log("SE",startDate,endDate)
return moment(this.parent.startDate).isSameOrBefore(moment(value))
}
推荐阅读
- google-bigquery - 使用 BigQuery SQL 语法匹配记录
- javascript - React.js + Socket.io 更新状态改变列表项的位置
- arrays - 在动态数组中使用二进制搜索搜索 typedef struct DATE
- docker - Docker 容器显示入口点命令“未找到”
- python - 如何按多个条件过滤熊猫数据框列
- ruby - Jekyll 安装问题。Jekyll 使用 bash 终端安装,但当我尝试检查版本时输出“jekyll:找不到命令”错误
- java - 由于不可访问的部署目录,Keycloak 启动时扫描失败:/opt/jboss/keycloak/standalone/deployments
- node.js - 404 gh-pages 单页 React App 部署后
- css - 从 YouTube 直播页面中删除边距
- android - Android:防止歌曲在 MediaPlayer 中循环播放。OnCompletionListener 未触发