how to convert, date change event into particular format

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') 

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

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

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?

Answers:

Answer

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

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

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'));

});
Answer

try this change .format('LL') to .format('x') i.e

var startDate_1 = moment.utc(startDateCal).format('x');
var endDate_1 = moment.utc(endDateCal).format('x');

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.