简体   繁体   English

如何在javascript datarangepicker中格式化moment.js日期?

[英]How I can format moment.js date in javascript datarangepicker?

i want to change the format of date (momentjs) from mm/ddYYYY to dd/mm/YYYY in daterangepicker. 我想在daterangepicker中将日期(momentjs)的格式从mm / ddYYYY更改为dd / mm / YYYY。 if I use the method .format(ddmmYYYY) it doesn't work 如果我使用方法.format(ddmmYYYY),它将不起作用

i try to change the method to format but it doesn't work well 我尝试将方法更改为格式化,但效果不佳

<script type="text/javascript">
        $(function() {

            //I want to change the format here
            var start = moment();
            var end = moment();

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

            $('#dashboard-report-range').daterangepicker({
                startDate: start,
                endDate: end,
                locale: {
                    "fromLabel": "From",
                    "toLabel": "To",
                    "customRangeLabel": "Modify",
                    cancelLabel: 'Clear',
                    applyLabel: 'Apply'
                },
                ranges: 
                {
                    //I want to change the format here for all elements
                   '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')]
                },
            }, cb);

            cb(start, end);

        });
        </script>

Actually the date is printed with format dd/mm/YYYY and if I try to change the format the output is NanNanNan 实际上日期以dd / mm / YYYY格式打印,如果我尝试更改格式,则输出为NanNanNan

You need to specify what format the date range picker will use by adding format: 'DD/MM/YYYY' to the daterangepicker's locale config: 您需要通过在daterangepicker的语言环境配置中添加以下format: 'DD/MM/YYYY'来指定日期范围选择器将使用的format: 'DD/MM/YYYY'

 $(function() { //I want to change the format here var start = moment(); var end = moment(); function cb(start, end) { $('#dashboard-report-range span').html(start + ' - ' + end); } $('#dashboard-report-range').daterangepicker({ startDate: start, endDate: end, locale: { "fromLabel": "From", "toLabel": "To", "customRangeLabel": "Modify", cancelLabel: 'Clear', applyLabel: 'Apply', format: 'DD/MM/YYYY' // <-- Add this line }, ranges: { //I want to change the format here for all elements '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')] }, }, cb); cb(start, end); }); 
 <link href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script> <input type="text" id="dashboard-report-range" /> 

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

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