Javascript获取日期并循环到组合框

时间:2013-04-28 11:56:05

标签: javascript jquery

对于这种情况,我不想使用jQuery日期时间选择器。因为我制作了移动应用程序。

[我的案例]我使用javascript来获取今天和接下来几天的价值。但我希望更灵活,并希望使用jquery在dilooping上显示数据并进入组合框。

到目前为止,我使用以下方式获取数据:

function GetDay(){
     var today = new Date();
     var dd = today.getDate();
     var mm = today.getMonth()+1; //January is 0!
     var yyyy = today.getFullYear();
     if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = yyyy+'-'+mm+'-'+dd;
     alert(today);
}

Result : 2013-04-28

对我来说,第二天使用:

function NextDay1(){
    var today = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();
    if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} next1 = yyyy+'-'+mm+'-'+dd;
    alert(next1);
}

Result : 2013-04-29

我使用的方式非常手动,如果我想获取数据:

2013-04-28
2013-04-29
2013-04-30
2013-05-01
2013-05-02
ect...

我使用的方式是非常手动的,如果我想获取这样的数据并放置一个组合框来选择那个日期?

3 个答案:

答案 0 :(得分:2)

您可以像这样在循环中获取日期:

// Create an empty array to store the dates generated
var myDates = [];

// Loop through for 5 times here
for (var i = 0; i < 5; i++) {
    var myDate = new Date();
    var fullDate = new Date(myDate.setDate(myDate.getDate() + i));
    var twoDigitMonth = ((fullDate.getMonth().length + 1) === 1) ? (fullDate.getMonth() + 1) : '0' + (fullDate.getMonth() + 1);
    var twoDigitDates = ('0' + fullDate.getDate()).slice(-2);
    var currentDate = fullDate.getFullYear() + "-" + twoDigitMonth + "-" + twoDigitDates;
    myDates.push(currentDate);
}

// Check the dates in console as an array
console.log(myDates);

然后插入下拉列表:

// Loop through the date array we have created
$.each(myDates, function (index, item) {

    // Add options to the select list
    $('#mySelect').append($('<option>', {
        value: index,
        text: item
    }));
});

答案 1 :(得分:2)

我们在这里有一个选择和匿名函数来填充它。 Date()在循环之外,因此只创建一次。填充现在是一个单独的功能。传递给匿名函数的数字是日期总数,包括今天。 FIddle

HTML:

<form id="someform">
    <select id="year"></select>
</form>

JS:

//did you spot the mistake here ? :) 10 is not bigger than 10 and we end with 010
//function pad(v) { return v > 10 ? v : "0" + v; }

//the right way
function pad(v) { return v < 10 ? "0" + v : v; }
(function (max) {
    var counter = 0,
        d = new Date(),
        myselect = document.getElementById("year");
    do {
        d.setDate(d.getDate() + 1);
        myselect.add(
            new Option (
                d.getFullYear() + "-" +
                pad(d.getMonth() + 1) + "-" +
                pad(d.getDate()), 
                counter++),
            null
        );
    } while (counter < max);
})(5);

答案 2 :(得分:1)

试试这个 -

var myDate = new Date();
var d = myDate.getFullYear() + '-' +  ('0' + (myDate.getMonth()+1)).slice(-2) + '-' + myDate.getDate();
var myDates = [d]; //initialize array with today's date already in it.

for (var i = 0; i < 5; i++) {        
    var fullDate = new Date(myDate.setDate(myDate.getDate() + 1)); //add 1 not i
    var twoDigitMonth = ('0' + (fullDate.getMonth() + 1)).slice(-2);
    var currentDate = fullDate.getFullYear() + "-" + twoDigitMonth + "-" + fullDate.getDate();
    myDates.push(currentDate);
}

console.log(myDates);

您接受的答案的问题是,添加i会更改myDate,并且由于i不断递增for循环,您会在日期中添加不同的值每一次。因此你得到了 -

["2013-04-28", "2013-04-29", "2013-05-1", "2013-05-4", "2013-05-8"]

每次日期增加不同的值。

使用我的代码 -

["2013-04-28", "2013-04-29", "2013-04-30", "2013-05-1", "2013-05-2", "2013-05-3"]