【问题标题】:Date comparision on cshtml viewcshtml视图中的日期比较
【发布时间】:2018-12-27 04:43:34
【问题描述】:

我正在使用下面的代码将 cshtml 视图上的 2 个日期与淘汰赛绑定进行比较。

data-bind="visible: (new Date(appointmentDate) - new Date() < 0) && isStart()"

它工作正常,但在比较时也包括时间。我不想在仅比较日期中包含时间。

【问题讨论】:

  • 据我所知,如果我错了,请纠正我。 Date 是一个 DateTime,时间设置为 00:00:00。如果在您的情况下,约会日期已经设定了时间。只需执行 New DateTime(appointmentDate).Date
  • 我的错实际上是 knockout.js 绑定。让我用完整的代码更新问题。

标签: asp.net-mvc-4 razor knockout.js knockout-mvc


【解决方案1】:

我在谷歌上快速搜索指向Formatting Date in Knockout Template 这将允许我们获取日期并进行比较。看起来像

data-bind="visible: (
    moment(new Date(appointmentDate)).format('MM/DD/YYYY') - 
    moment(new Date()) < 0) && isStart()"

我没有尝试,只是让我知道是否有效

momento 还允许您计算日期的差异

var dateB = moment('2014-11-11');
var dateC = moment('2014-10-11');

console.log('Difference is ', dateB.diff(dateC), 'milliseconds');
console.log('Difference is ', dateB.diff(dateC, 'days'), 'days');
console.log('Difference is ', dateB.diff(dateC, 'months'), 'months');

所以基本上我们会这样做

data-bind="visible: (
        moment(new Date(appointmentDate)).format('MM/DD/YYYY').diff(new Date().format('MM/DD/YYYY'),'days') < 0) && isStart()"

【讨论】:

  • 格式化正在处理这个问题,但我想减法在放置时刻后不起作用。
  • 正确需要使用 diff。感谢您的帮助。
  • (moment(new Date(appointmentDate)).diff(moment(new Date()),'days')
猜你喜欢
  • 1970-01-01
  • 2016-11-16
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 2018-11-24
  • 2016-07-25
相关资源
最近更新 更多