【问题标题】:moment.js cannot convert two strings to onemoment.js 无法将两个字符串转换为一个
【发布时间】:2016-03-24 14:26:01
【问题描述】:
import moment from 'moment';

export class DateTimeFormat {
    format(date, time) {
        alert(date);

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

'Date' 可以作为字符串 'DD/MM/YYYY' (来自加载时的数据库)或来自日历 (this.value = $('.input-group.date', this.element).datepicker('getUTCDate');) 日期选择器的日期对象在自定义元素中使用。

DateTimeFormat 是这样使用的:

            .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()

我尝试过: moment.(date) - 无效日期失败 moment.utc(date) - 无效日期失败

不知道是否需要添加try catch in?即尝试从日期对象转换,如果它在捕获范围内,那么它必须是来自数据库等的字符串?

使用来自@VicenzoC 的以下信息,我添加了一个 if 语句

var dateFormatted;

if (typeof(date) === 'string')
{
    dateFormatted = date;
}
else
{
    // Must be an object from a calendar etc
    alert(date);
    dateFormatted = moment(date, 'DD/MM/YYYY');
    alert(dateFormatted);
}

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

第一个警报 -

第二次警报 -

【问题讨论】:

    标签: datetime datepicker momentjs


    【解决方案1】:

    在我看来,您的 .datepicker('getUTCDate') 返回一个 Date 对象,在这种情况下,您可以使用 moment(Date); 将其解析为 moment 对象

    this.value = $('.input-group.date', this.element).datepicker('getUTCDate');
    var momentObj = moment(this.value);
    

    如果你有一个字符串,你可以使用:

    拥有moment 对象后,如果您需要以自定义格式显示日期,则可以使用format 方法。

    【讨论】:

    • 感谢@VicenzoC,当页面首次加载时,数据是从数据库中提取的,因此只有在从日历中选择它时才会返回日期对象.我是不是最好做类似 try/catch 语句的事情?
    • @VicenzoC - 更新了我的问题,因为我认为我的解释不正确。
    • @ClareBarrington 也许您可以使用typeof 运算符来确定您的输入变量类型,然后调用合适的解析函数。
    • 感谢@VicenzoC。我更新了我的代码以使用 if 语句,并更新了我的问题
    • 感谢您的帮助@VicenzoC
    【解决方案2】:
    import moment from 'moment';
    
    export class DateTimeFormat {
        format(date, time) {
    
            var dateFormatted;
    
            if (typeof(date) === 'string')
            {
                dateFormatted = date;
            }
            else
            {
                // Must be an object from a calendar etc
                dateFormatted = moment(date).format('DD/MM/YYYY');
            }
    
            return moment(dateFormatted + 'T' + time, 'DD/MM/YYYY HH:mm:ss');
        }
    }
    

    这是最后的代码,我首先确定 typeof(再次感谢 @VicenzoC 为我指出该方向),然后我处理 if 语句中的日期部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 2018-08-27
      • 2011-12-22
      • 2012-02-21
      • 1970-01-01
      • 2013-03-12
      相关资源
      最近更新 更多