Aurelia 验证一个日期时间是否大于另一个

Aurelia validate one datetime is greater than the other

我有两个来自数据库的 date/times,但是在视图中它们被分成四个(两个日期和两个时间),日期和时间都是自定义元素。见下文。

我的问题是我的开始 date/time 不能大于我的结束 date/time。我知道如何在 c# 中执行此操作:

但我不知道如何在不进行 API 调用的情况下在 Aurelia 中执行此操作。我已经注意到 isLessThan,但是我需要先将我的字符串解析为日期时间才能执行此操作。

      bind(){
            return this.dataContext.getContent(this.id)
                .then(baseContent => {
                    this.baseContent = baseContent;
                     this.validator = this.validation.on(this)                                         
    .ensure('baseContent.ValidFromDate').isNotEmpty()
                            .ensure('baseContent.ValidFromTime').isNotEmpty()
                            .ensure('baseContent.ValidToDate').isNotEmpty()
                            .ensure('baseContent.ValidToTime').isNotEmpty()
.ensure('baseContent.ValidFromDate / baseContent.ValidFromTime').isLessThan('baseContent.ValidToDate / baseContent.ValidToTime')
;
                }); }

我知道上面的方法行不通,我是 Aurelia 的新手,还在适应它。

更新

我尝试了以下方法但收到了:

Unhandled promise rejection TypeError: path.split is not a function

我认为问题是它不知道将错误消息分配给什么,但我可能是错的。

.ensure(this.datetimeformat.format(baseContent.ValidFromDate, baseContent.ValidFromTime)).isLessThan(this.datetimeformat.format(baseContent.ValidToDate, baseContent.ValidFromTime));

import moment from 'moment';

export class DateTimeFormat {
    format(date, time) {

        if (date.indexOf("T") > -1) {
            date = date.split('T')[0];
        }

        return moment(date + 'T' + time, 'DD/MM/YYYY HH:mm:ss');
    }
}

我注意到其中一个答案使用了 computedFrom,但是因为我需要先将它们更改为 DateTime,然后再解析回来,我不知道我打算在哪里提供什么。

更新

.ensure('baseContent.ValidFromDate', (config) => { config.computedFrom(['baseContent.ValidFromDate', 'baseContent.ValidFromTime', 'baseContent.ValidToDate', 'baseContent.ValidToTime']) }).isNotEmpty().passes(() => {
                        return this.datetimeformat.format(this.baseContent.ValidFromDate, this.baseContent.ValidFromTime) < this.datetimeformat.format(this.baseContent.ValidToDate, this.baseContent.ValidToTime);
                    })

我想我越来越接近了,但仍然没有工作:-(

我也试过了

.ensure('baseContent.ValidFromDate', (config) => { config.computedFrom(['baseContent.ValidFromDate', 'baseContent.ValidFromTime', 'baseContent.ValidToDate', 'baseContent.ValidToTime']) }).if(() => {
                        return this.baseContent.ValidFromDate !== null && this.baseContent.ValidFromTime !== null && this.baseContent.ValidToDate !== null && this.baseContent.ValidToTime !== null})
                        .passes( () => {return this.datetimeformat.format(this.baseContent.ValidFromDate, this.baseContent.ValidFromTime) < this.datetimeformat.format(this.baseContent.ValidToDate, this.baseContent.ValidToTime)})
                    .endIf().isNotEmpty()

开发者工具没有错误。

function validateFromDateTimeIsBeforeToDateTime(dateFrom, timeFrom, dateTo, timeTo) {
        debugger;
        if (dateFrom !== null && dateFrom !== undefined &&
        timeFrom !== null && timeFrom !== undefined &&
        dateTo !== null && dateTo !== undefined &&
        timeTo !== null && timeTo !== undefined) {

            return this.datetimeformat.format(dateFrom, timeFrom) < this.datetimeformat.format(dateTo, timeTo);
    } 
    else {
        return true;
    }
}

                    .ensure('baseContent.ValidFromDate').isNotEmpty().passes(validateFromDateTimeIsBeforeToDateTime(this.baseContent.ValidFromDate, this.baseContent.ValidFromTime, this.baseContent.ValidFromDate, this.baseContent.ValidFromTime))
                    .ensure('baseContent.ValidFromTime').isNotEmpty()
                    .ensure('baseContent.ValidToDate').isNotEmpty().passes(validateFromDateTimeIsBeforeToDateTime(this.baseContent.ValidFromDate, this.baseContent.ValidFromTime, this.baseContent.ValidFromDate, this.baseContent.ValidFromTime))
                    .ensure('baseContent.ValidToTime').isNotEmpty()

然而,这会引发 "Unhandled promise rejection TypeError: Unable to get property 'datetimeformat' of undefined or null reference"

在这种情况下,我建议使用无处不在的 moment.js 库:

jspm install moment

然后像这样使用它例如:

import moment from 'moment';

export class MyViewModel {
  date1 = "2016-02-29";
  time1 = "09:00:00";
  date2 = "2016-03-01";
  time2 = "13:00:00";

  myMethod() {
    let isValid = moment(`${this.date1 this.time1`).isBefore(moment(`${this.date2 this.time2`));
    alert(isValid);
  }
}
.ensure('toDate', (config) => {config.computedFrom(['fromDate'])})
   .isGreaterThan( () => {return this.fromDate}, 'the entered Date');

经过大量调查,修复了各种错误,我注意到突出的问题是我的格式设置 class,它没有创建 date/time(使用 moment.js) 正确(在用户从日历中选择之后),这意味着它返回了 'invalid date',您无法在 :-|

上进行比较

这会起作用:

this.validator = this.validation.on(this)
                    .ensure('baseContent.ValidFromDate', (config) => { config.computedFrom(['baseContent.ValidFromDate', 'baseContent.ValidFromTime', 'baseContent.ValidToDate', 'baseContent.ValidToTime']) })
                        .if(() => {
                            return this.baseContent.ValidFromDate !== null && this.baseContent.ValidFromTime !== null && this.baseContent.ValidToDate !== null && this.baseContent.ValidToTime !== null })
                            .passes( () => { return this.datetimeformat.format(this.baseContent.ValidFromDate, this.baseContent.ValidFromTime) < this.datetimeformat.format(this.baseContent.ValidToDate, this.baseContent.ValidToTime) })
                            .withMessage('< Valid To')
                        .endIf()