没有使用javascript获取日期格式

我希望在2个日期之间获得所有日期。 所以在这里我提到statdate是日期和结束日期是daydate。 在两个日期之间,我想要所有日期。

实际上我得到所有日期但不正确格式,我想要的格式为DD/MM/YY

现在我进入默认格式(2007年6月9日星期六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")); } }); 

你可以这样做

 $("#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) }) 

这里'between'是包含您所需的所有日期的数组。请在此处查看演示

  alert("Dates!!!" + aDates.getDate()+"/"+ (aDates.getMonth()+1)+"/"+ aDates.getFullYear()); 

您似乎希望以相同格式给出输入字符串,以d / m / y格式获取日期字符串数组。 以下function将执行此操作。

 // Parse a string in dmy format // return a date object, NaN or undefined function parseDMY(s) { var b = s.match(/\d+/g); if (b) { return new Date(b[2], --b[1], b[0]); } } // Given a date object, return a string in dd/mm/yyyy format function formatDMY(date) { function z(n){return (n<10? '0' : '') + n;} return z(date.getDate()) + '/' + z(date.getMonth() + 1) + '/' + date.getFullYear(); } function getWeekDates(s) { var d = parseDMY(s); var dates = []; if (d) { for (var i=0; i<7; i++) { dates.push(formatDMY(d)); d.setDate(d.getDate() + 1); } return dates; } } console.log(getWeekDates('7/7/2014').join()); // 07/07/2014,08/07/2014,09/07/2014,10/07/2014,11/07/2014,12/07/2014,13/07/2014 

请注意,在日期中添加1天优先于添加毫秒,因为它允许Date对象考虑可能涉及的夏令时更改。