简体   繁体   English

日期范围选择器插件日期格式问题

[英]Date Range Picker Plugin Date Format Issue

I'm having an issue where the Date Range Picker plugin (via moment.js) is converting my date format from (MM/DD/YYYY) to the Unix date format. 我遇到了一个问题,其中日期范围选择器插件(通过moment.js)将我的日期格式从(MM / DD / YYYY)转换为Unix日期格式。

To recreate the issue, I created a Fiddle. 为了重现问题,我创建了一个小提琴。 In the Fiddle, click on the date range and the picker will display. 在小提琴中,单击日期范围,然后将显示选择器。 In the picker, select a date range and click "apply". 在选择器中,选择一个日期范围,然后单击“应用”。 When you do this, you will note that the date range format is now in the Unix format. 执行此操作时,您会注意到日期范围格式现在为Unix格式。

How do I convert the date format back to 'MM/DD/YYYY' when I click apply? 单击“应用”后,如何将日期格式转换回“ MM / DD / YYYY”?

HTML HTML

 <div id="daterange"><span></span></div>

JQUERY JQUERY

$(function() {
    var listItem, applyClicked = false,
        start = '10/10/2016',
        end = '12/05/2016';

    function cb(start, end) {
        $('#daterange span').html(start + ' - ' + end);
    }

    //var num = $("#daterange").data("datepicker");
    //cb(moment().subtract(num, 'days'), moment());

    $('#daterange').daterangepicker({
        ranges: {
            'Today': [moment(), moment()],
            'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            'Last 7 Days': [moment().subtract(6, 'days'), moment()],
            'Last 30 Days': [moment().subtract(29, 'days'), moment()],
            'This Month': [moment().startOf('month'), moment().endOf('month')],
            'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        locale: {
            format: 'MM/DD/YYYY'
        },
        opens: "left",
        startDate: start,
        endDate: end,
        maxDate: moment().endOf("day"),
        alwaysShowCalendars: true,
        autoUpdateInput: true
    }, cb);

    cb(start, end);

    // Dont close daterangepicker immediately when predefined range selected
    $(".ranges ul li").click(function() {
        listItem = $(this).text();
    });

    $(".range_inputs").click(function() {
        applyClicked = true;
    });

    $('#daterange').on('apply.daterangepicker', function(ev, picker) {
        //var test = moment.unix(startDate).format("MM/DD/YYYY");
        //$('#daterange span').html(test + ' - ' + end);
        if (listItem != "Custom Range" && !applyClicked) {
            picker.show();
            applyClicked = false;
        }
    });
});

Fiddle https://jsfiddle.net/coryspi/oka1noht/ 小提琴 https://jsfiddle.net/coryspi/oka1noht/

Thanks in advance for your help. 在此先感谢您的帮助。

Just wrap your start and end values by moment, then format it to MM/DD/YYYY : 只需按时包装您的开始和结束值,然后将其格式化为MM/DD/YYYY

function cb(start, end) {
    $('#daterange span').html(moment(start).format('MM/DD/YYYY') + ' - ' + moment(end).format('MM/DD/YYYY'));
}

Take a look on a Working fiddle: https://jsfiddle.net/6w2m83qa/ 看看工作的小提琴: https : //jsfiddle.net/6w2m83qa/

Note that when you wrap in moment, it try to understand what format you are using. 请注意,当您换行时,它会尝试了解您使用的格式。 MM/DD/YYYY runs fine, Unix timestamp too. MM/DD/YYYY运行正常, Unix timestamp也是如此。

If your format wasn't any of the supported formats, eg. 如果您的格式不是任何受支持的格式,例如。 DD/MM/YYYY , you should need to specify the input format. DD/MM/YYYY ,您需要指定输入格式。

Replace your call back function with this 以此替换您的回叫功能

function cb(start, end) {
    $('#daterange span').html(moment(start).format('MM/DD/YYYY') + ' - ' + moment(end).format('MM/DD/YYYY'));
}

Its just you have to change the format. 它只是您必须更改格式。

Hope it helps. 希望能帮助到你。

As you can see in the config documentation your cb function is of type: 如您在配置文档中所见,您的cb函数类型为:

function(startDate, endDate, label) {

It's a callback function triggered from daterangepicker when the user chooses new dates. 当用户选择新日期时,这是从daterangepicker触发的回调函数。

The first two arguments are moment objects. 前两个参数是矩对象。

So, your function becomes: 因此,您的功能变为:

function cb(start, end) {
    $('#daterange span').html(start.format('MM/DD/YYYY') + ' - ' + end.format('MM/DD/YYYY'));
}

and you can call it at dom ready as: 您可以在dom准备就绪时将其称为:

cb(moment(start), moment(end));

The updated fiddle 更新的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM