【问题标题】:MomentJS returning unexpected resultMomentJS 返回意外结果
【发布时间】:2021-08-10 00:52:43
【问题描述】:

不确定我的日期和 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))
}

startDateendDate 的 console.log 的输出是:

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 之后的情况

我错过了什么?

【问题讨论】:

  • 不要比较格式化日期,比较原始日期。

标签: javascript date momentjs


【解决方案1】:

比较原始日期,而不是格式化日期,因为 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))
}

【讨论】:

    猜你喜欢
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 2016-02-24
    相关资源
    最近更新 更多