如何将日期更改事件转换为特定格式 [英] how to convert, date change event into particular format

查看:154
本文介绍了如何将日期更改事件转换为特定格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有日期,并且我使用的是daterangepicker和moment js.现在,我对所有这些事物还是陌生的.变量设置日期之一为

I am having date in my site, and I am using dates using daterangepicker and moment js. Now I am new to all these things & one of the variable setting up date is as

moment().subtract(29, 'days') 

console.log结果为1492930351644moment()结果与1495435951644类似.

whose console.log results into 1492930351644, similarly moment() result to 1495435951644.

现在我要由用户手动更改,以更新此值,但是我不知道如何使用它.我使用了如下的jQuery事件

Now on manual change by the user I want, to update this values but however I don't know how to make use of it. I have used jQuery event as below

$('#dateRange').on('apply.daterangepicker', function(ev, picker) {
    var startDate_1 = moment.utc(startDateCal).format('LL');
    var endDate_1 = moment.utc(endDateCal).format('LL');

    dates.setData('startDateCal',startDate_1);  
    dates.setData('endDateCal',endDate_1); 

    console.log(" changed start date "+dates.getData('startDateCal'));
    console.log(" changed end date "+dates.getData('endDateCal'));

});

因此,当我更改并从视图日历中选择今天作为今天时,我会进入上述更改事件,但我的输出将是

so when I change and select the date as today from view calender I am get into the above change event but my output would be

 changed start date May 21, 2017
 changed end date May 22, 2017

在这里,我想要一个类似于1492930351644的输出或日​​期格式,如moment().subtract(29, 'days')所示,但我不知道该如何实现.有帮助吗?

Here I want an output or date format similar to 1492930351644 as shown by moment().subtract(29, 'days') but I don't know how would I be able to achieve that. Any help?

推荐答案

您想要将Unix ms时间戳格式另存为data-属性.您做对了,只需要使用 format date 函数像这样:

You want to save the unix ms timestamp format as a data- attribute. You are doing it right, you just have to use the format date function like this:

moment.utc(startDateCal).format('x') // get the unix timestamp format

或者,当您读回数据时,应该从中创建一个矩对象,然后使用valueOf获取数字格式:

Or when you read the data back, you should create a moment object out of it and use valueOf to get the number format:

moment(dates.getData('startDateCal')).valueOf()

似乎您当前正在使用固定的startDateCalendDateCal.我建议您在回调函数中使用picker属性,从Picker本身获取实际日期,如下所示:

It seems like you are currently using a fixed startDateCal and endDateCal. I suggest to you to use the picker attribute in the callback function to get the actual date from the picker itself, like this:

$('#dateRange').on('apply.daterangepicker', function(ev, picker) {
    var startDate_1 = picker.startDate.format('x');
    var endDate_1 = picker.endDate.format('x');

    dates.setData('startDateCal',startDate_1);
    dates.setData('endDateCal',endDate_1);

    console.log(" changed start date "+dates.getData('startDateCal'));
    console.log(" changed end date "+dates.getData('endDateCal'));

});

这篇关于如何将日期更改事件转换为特定格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆