无法使用JavaScript获取日期格式 [英] Not getting date format using javascript

查看:89
本文介绍了无法使用JavaScript获取日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取2个日期之间的所有日期.所以在这里我提到了statdate是日期,而结束日期是weekdate.在2个日期之间,我需要所有日期.

I want to get all dates in between 2 dates. So here I have mentioned statdate is date and end date is weekdate. In between 2 dates I want all dates.

实际上,我正在获取所有日期,但格式不正确,我想要的是这种格式DD/MM/YY.

Actully I am getting all dates But Not proper Format ,what i want in this format DD/MM/YY.

现在我将使用默认格式(2007年6月9日星期六17:46:21)

Now I am Getting in default Format (Sat Jun 09 2007 17:46:21)

$(document).ready(function () {
    $("#day").click(function () {
        startJsonSession();
        return false;
    });
    function startJsonSession() {
        var inputdate = $('#inputdate').val();
        //alert("Input Date!!!" + inputdate );
        var d = new Date(inputdate);
        var nowMS = d.getTime(); // get # milliseconds for today
        //alert(nowMS);
        var week = 1000 * 60 * 60 * 24 * 7; // milliseconds in one week
        //alert(week);
        var oneWeekFromNow = new Date(nowMS + week);
        //alert("oneWeekFromNow!!!" + oneWeekFromNow);
        var fromdate = d.getDate();
        var month = d.getMonth() + 1;
        var year = d.getFullYear();
        if (fromdate < 10) {
            fromdate = "0" + fromdate;
        }
        if (month < 10) {
            month = "0" + month;
        }
        //var date = fromdate + "/" + month + "/" + year;
        var date = year + "/" + month + "/" + fromdate;
        alert("InputDate!!!!" + date);

        //var weekdate=oneWeekFromNow.getDate() + "/" + month + "/" + year;
        var weekdate = year + "/" + month + "/" + oneWeekFromNow.getDate();
        alert("weekdate!!!" + weekdate);
        var tomorrow = new Date(d.getTime() + (24 * 60 * 60 * 1000));
        var tomorrowdate = tomorrow.getDate();
        var month1 = tomorrow.getMonth() + 1;
        var year1 = tomorrow.getFullYear();
        if (tomorrowdate < 10) {
            tomorrowdate = "0" + tomorrowdate;
        }
        if (month1 < 10) {
            month1 = "0" + month1;
        }
        //var nextday = tomorrowdate + "/" + month1 + "/" + year1;
        var nextday = year1 + "/" + month1 + "/" + tomorrowdate;
        alert("tomorrow!!!!" + nextday);
        var d1 = new Date(date);
        alert("D1!!!!!" + d1.);
        var d2 = new Date(weekdate);
        var aDates = [];
        do {
            aDates.push(d1.toString());
            d1.setDate(d1.getDate() + 1);
        }
        while (d1 <= d2);
        alert("Dates!!!" + aDates);
        //alert(aDates.join("\n")); 
    }
});

推荐答案

您可以通过这种方式完成

You can do it in this way

$("#getDate").click(function () {
    var start = $("#startdate").datepicker("getDate"),
        end = $("#enddate").datepicker("getDate");

    currentDate = new Date(start),
    between = [];
    while (currentDate < end) {
        between.push(new Date(currentDate));
        currentDate.setDate(currentDate.getDate() + 1);

    }

    for (var i = 0; i < between.length; i++) {
        var date = $.datepicker.formatDate('dd/mm/yy', new Date(between[i]));
        between[i] = date;
    }
    console.log(between)
})

在...之间" 是包含您所有必需日期的数组 在此处查看演示

Here 'between' is the array which contains all your required Date SEE DEMO HERE

这篇关于无法使用JavaScript获取日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆