简体   繁体   English

jQuery日期选择器选择的日期格式

[英]jquery date picker selected date format

In my date script mentioned below when I select a month and year format it will show all the dates in that month. 在下面提到的日期脚本中,当我选择月份和年份格式时,它将显示该月的所有日期。 It's working fine but I want to dispaly the output like 2017-09-08 but it outputs like this 2017-9-8 它工作正常,但我想显示2017-09-08这样的输出,但它输出像这样的2017-9-8

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css">
<label for="startDate">Date :</label>
    <input name="startDate" id="startDate" class="date-picker" />
    <div id="datecontent">
    </div>

<script type="text/javascript">
var weekday = new Array(7);
        weekday[0] = "Sunday";
        weekday[1] = "Monday";
        weekday[2] = "Tuesday";
        weekday[3] = "Wednesday";
        weekday[4] = "Thursday";
        weekday[5] = "Friday";
        weekday[6] = "Saturday";
$(function() {
    $('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        minDate: 0,
        onClose: function(dateText, inst) { 
            $d = new Date(inst.selectedYear, parseInt(inst.selectedMonth)+1, 0).getDate();
            $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
            html='';
            for(i=1;i<=$d;i++){
                console.log(inst.selectedYear+'-'+(parseInt(inst.selectedMonth)+1)+'-'+i);
                d = new Date(inst.selectedYear+'-'+(parseInt(inst.selectedMonth)+1)+'-'+i);
                console.log(d);
                n = weekday[d.getDay()];
                html += '<div class="datediv">div-'+i+'<br>'+n+'</div>';
            }
            $('#datecontent').html(html);
        }
    });
</script>

My Output 我的输出

Console.log Console.log

2017-9-1
Fri Sep 01 2017 00:00:00 GMT+0530 (IST)
2017-9-2
Sat Sep 02 2017 00:00:00 GMT+0530 (IST)
2017-9-3
Sun Sep 03 2017 00:00:00 GMT+0530 (IST)
2017-9-4
Mon Sep 04 2017 00:00:00 GMT+0530 (IST)
2017-9-5
Tue Sep 05 2017 00:00:00 GMT+0530 (IST)
2017-9-6
Wed Sep 06 2017 00:00:00 GMT+0530 (IST)
2017-9-7
Thu Sep 07 2017 00:00:00 GMT+0530 (IST)
2017-9-8
Fri Sep 08 2017 00:00:00 GMT+0530 (IST)
2017-9-9
Sat Sep 09 2017 00:00:00 GMT+0530 (IST)
2017-9-10
Sun Sep 10 2017 00:00:00 GMT+0530 (IST)

How can I achieve this? 我该如何实现?

Before I answer your question, I would like to mention that time and date handling is a lot deeper than it may first appear, and that generally it's bad practice to reinvent the wheel . 在回答您的问题之前,我想提一下时间和日期的处理要比第一次出现的要深得多,并且通常来说, 重新发明轮子是不好的做法。 Consider using the excellent moment.js to parse, process and display your dates. 考虑使用出色的moment.js来解析,处理和显示您的日期。

With that out of the way, let's zero-pad those dates. 不用担心,让我们将这些日期归零。

You're using parseInt which parses a string into an integer. 您正在使用parseInt将字符串解析为整数。 However, 01, 02, 03 etc are not valid integers - you will need to output strings. 但是,01、02、03等不是有效的整数 -您将需要输出字符串。

One way to do this is: 一种方法是:

function zeroPad(number)
{
    return ('0' + String(number)).slice(-2);
}

Which you can then call like: 然后可以像这样调用它:

console.log(inst.selectedYear+'-'+zeroPad(parseInt(inst.selectedMonth)+1)+'-'+zeroPad(i));

Note that the String() call isn't necessary (JavaScript will implicitly typecast number to a string), but I prefer to be explicit with these things. 请注意,不需要String()调用(JavaScript会隐式地将数字类型转换为字符串),但是我更希望在这些事情上保持明确。

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

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