【问题标题】:using a php unix timestamp with moment.js使用带有 moment.js 的 php unix 时间戳
【发布时间】:2014-07-08 20:41:25
【问题描述】:

似乎无法让 unix 时间戳与 moment.js 一起使用...

在这种情况下,date_create 等于我的 php unix 时间戳,我想将其用作 startDate、minDate 和其他一些地方......我没有收到错误,但它显示为“12 月 31 日, 1969 @ 7:00 pm' 这显然是不正确的。

我在这里做错了什么?目标是从给定的 utc 时间戳将正确的日期/时间显示为计算机时间。

例如 1401079499 是 05 / 26 / 14 @ 4:44:59am UTC,但这将显示为 May 26, ‎2014‎ ‎12‎:‎44‎:‎59‎ am ‎在我自己的计算机上是 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();

};

编辑 - 解决方案:

我忘记更改我从中获取 unix 时间戳的 id...$('#dashboard-report-range #date-create').html(); 有了这个更改,一切正常。

【问题讨论】:

    标签: momentjs


    【解决方案1】:

    将时间戳乘以 1000。 PHP 以秒为单位返回时间戳,但 JS 将其理解为毫秒。

    【讨论】:

    • 是的,我知道... moment.unix(date_create) 根据他们的文档自动执行此操作。
    • date_create 的值是多少?
    • 它在帖子中出现了两次 - 等于 1401079499 - 一个 unix 时间戳
    • 对不起,我不想打扰你。我认为您应该再次检查该值,因为如果它是正确的,您应该得到这样的预期结果:jsbin.com/judamuja/2/edit
    • 忘记更改 #id 我从中获取值 - $('#dashboard-report-range #date-create').html();...一切都很好...谢谢!
    猜你喜欢
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多