日期格式问题。 js jquery

时间:2017-08-02 00:32:17

标签: javascript jquery

7月之后的日期格式问题。显示的日期是每周格式。任何帮助将受到高度赞赏。日期工作正常,只是没有按预期工作

$(document).ready(function () {
        var curr = new Date; // get current date
        var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
        var last = first + 6; // last day is the first day + 6
        var startDate = new Date(curr.setDate(first));
        startDate = ((startDate.getMonth() + 1) < 10 ? '0'
                : '')
                + (startDate.getMonth() + 1)
                + "/"
                + ((startDate.getDate() < 10 ? '0' : '') + startDate
                        .getDate())
                + "/"

1 个答案:

答案 0 :(得分:2)

只需更改此代码

 var endDate = new Date(startDate);
 endDate.setDate(startDate.getDate() + 6);

您可以查看以下工作代码。

&#13;
&#13;
   

$(document).ready(function () {
        //var curr = new Date('2020-02-29'); // for leap
        var curr = new Date();// get current date
        var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
        var startDate = new Date(curr.setDate(first));
        var endDate = new Date(startDate);
        endDate.setDate(startDate.getDate() + 6);
        startDate = ((startDate.getMonth() + 1) < 10 ? '0'
                : '')
                + (startDate.getMonth() + 1)
                + "/"
                + ((startDate.getDate() < 10 ? '0' : '') + startDate
                        .getDate())
                + "/"
                + startDate.getFullYear();

        endDate = ((endDate.getMonth() + 2) < 10 ? '0' : '')
        //this might have some flaws if i make it to 2 it works but this is short term fix and will break again
                + (endDate.getMonth() + 1)
                + "/"
                + ((endDate.getDate() < 10 ? '0' : '') + endDate
                        .getDate())
                + "/"
                + endDate.getFullYear();

        document.getElementById("ok").innerHTML = startDate;
        document.getElementById("napa").innerHTML = endDate;

        $(".next")
                .click(
                        function () {
                            document.getElementById("tabletbody").innerHTML = "";
                            var startdt = new Date($('#ok')
                                    .text());
                            startdt.setDate(startdt
                                    .getDate() + 7);
                            document.getElementById("ok").innerHTML = (getDateFormat(startdt));

                            var enddt = new Date($('#napa')
                                    .text());
                            enddt
                                    .setDate(enddt
                                            .getDate() + 7);
                            document.getElementById("napa").innerHTML = (getDateFormat(enddt));
                            updateCompass();

                            return false;

                        });

        function getDateFormat(d) {
            var month = ((d.getMonth() + 1) < 10 ? '0' : '')
                    + (d.getMonth() + 1);
            var dd = (d.getDate() < 10 ? '0' : '')
                    + d.getDate();
            return month + "/" + dd + "/" + d.getFullYear();

        }

        $(".previous").click(function () {
            document.getElementById("tabletbody").innerHTML = "";
            var startdt = new Date($('#ok').text());
            startdt.setDate(startdt.getDate() - 7);

            $('#ok').text(getDateFormat(startdt));
            var enddt = new Date($('#napa').text());
            enddt.setDate(enddt.getDate() - 7);

            $('#napa').text(getDateFormat(enddt));
            updateCompass();

            return false;
        });
    });
&#13;
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<div id="ok"></div>
<div id="napa"></div>
&#13;
&#13;
&#13;