using a php unix timestamp with moment.js

2019-09-09 15:30发布

问题:

Can't seem to get unix timestamps working with moment.js...

date_create is equal to my php unix timestamp in this case and I want to use that as the startDate, minDate, and a few other places... I am not getting an error, but it is displaying as 'December 31, 1969 @ 7:00 pm' which is obviously not correct.

What am I doing wrong here? The goal is to have the correct date/time shown as computers time from the utc timestamp given.

For instance 1401079499 is 05 / 26 / 14 @ 4:44:59am UTC, but this would show as May 26, ‎2014‎ ‎12‎:‎44‎:‎59‎ am ‎on my own computer being gmt-4

    var date_create = $('#dashboard-report-range #date-display').html(); //equals 1401079499

    //date range stuff
    $('#dashboard-report-range').daterangepicker({
        opens: 'left',
        startDate: moment.unix(date_create).startOf('day'),
        endDate: moment().endOf('day'),
        minDate: moment.unix(date_create).startOf('day'),
        maxDate: moment().endOf('day'),
        showDropdowns: false,
        showWeekNumbers: true,
        timePicker: true,
        timePickerIncrement: 1,
        timePicker12Hour: true,
        ranges: {
            'All': [moment.unix(date_create).startOf('day'), moment().endOf('day')],
            'Today': [moment().startOf('day'), moment().endOf('day')],
            'Yesterday': [moment().subtract('days', 1).startOf('day'), moment().subtract('days', 1).endOf('day')],
            'Last 7 Days': [moment().subtract('days', 6).startOf('day'), moment().endOf('day')],
            'Last 30 Days': [moment().subtract('days', 29).startOf('day'), moment().endOf('day')],
            'This Month': [moment().startOf('month'), moment().endOf('month')],
            'Last Month': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
        },
        buttonClasses: ['btn'],
        applyClass: 'btn-sm btn-success',
        cancelClass: 'btn-sm btn-default',
        format: 'MM/DD/YYYY',
        //format: 'MM-DD-YY',
        separator: ' to ',
        locale: {
            applyLabel: 'Apply',
            fromLabel: 'From',
            toLabel: 'To',
            customRangeLabel: 'Custom Range',
            daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
            monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
            firstDay: 1
        }
    },
    function (start, end) {
        $('#dashboard-report-range #date-display').html('<span class="ddate_start">' + start.format('MMMM D, YYYY [@] h:mm a') + '</span> &nbsp;<i class="fa fa-arrows-h"></i>&nbsp; <span class="ddate_end">' + end.format('MMMM D, YYYY [@] h:mm a') + '</span>');
        $('#dashboard-report-range #date-start').html( start.unix() );
        $('#dashboard-report-range #date-end').html( end.unix() );
        dt.draw();
    }
    );

    $('#dashboard-report-range #date-display').html('<span class="ddate_start">' + moment.unix(date_create).startOf('day').format('MMMM D, YYYY [@] h:mm a') + '</span> &nbsp;<i class="fa fa-arrows-h"></i>&nbsp; <span class="ddate_end">' + moment().endOf('day').format('MMMM D, YYYY [@] h:mm a') + '</span>');
    $('#dashboard-report-range').show();

};

EDIT - solution :

I had forgotten to change the id I grab the unix timestamp from... $('#dashboard-report-range #date-create').html(); With that change everything works as it should.

回答1:

Multiply the timestamp by 1000. PHP returns timestamp in seconds, but JS understands it as milliseconds.



标签: momentjs