简体   繁体   English

Javascript 或 jQuery 中的日期、月份和年份

[英]Date,Month and year in Javascript or jQuery

I have javascript code for 3 dropdowns of Day, Month and Year as follows for date of birth:对于出生日期,我有 3 个日、月和年下拉列表的 javascript 代码,如下所示:

var monthtext = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];

function populatedropdown(dayfield, monthfield, yearfield) {
    var today = new Date()
    var dayfield = document.getElementById(dayfield)
    var monthfield = document.getElementById(monthfield)
    var yearfield = document.getElementById(yearfield)
    for (var i = 0; i < 31; i++)
        dayfield.options[i] = new Option(i, i + 1)
    dayfield.options[today.getDate()] = new Option(today.getDate(), today.getDate(), true, true) //select today's day
    for (var m = 0; m < 12; m++)
        monthfield.options[m] = new Option(monthtext[m], monthtext[m])
    monthfield.options[today.getMonth()] = new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
    var thisyear = today.getFullYear()
    for (var y = 0; y < 20; y++) {
        yearfield.options[y] = new Option(thisyear, thisyear)
        thisyear += 1
    }
    yearfield.options[0] = new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
}

The year is displaying 20 ascending order.年份按升序显示 20。 I need to make it 100 in descending order.我需要按降序将其设为 100。

How can i change it in the for loop?如何在 for 循环中更改它? I tried and got an infinite loop.我尝试并得到了一个无限循环。

Is there any jquery code similar to this other than datepicker ??除了 datepicker 之外,是否还有与此类似的 jquery 代码?

Try this, not tested:试试这个,未测试:

for (var y = 100; y >= 0; y--) {
    yearfield.options[y] = new Option(thisyear, thisyear)
    thisyear -= 1
}

Try this function试试这个功能

function populatedropdown(dayfield, monthfield, yearfield) {
var today = new Date()
var dayfield = document.getElementById(dayfield)
var monthfield = document.getElementById(monthfield)
var yearfield = document.getElementById(yearfield)
for (var i = 0; i < 31; i++)
    dayfield.options[i] = new Option(i, i + 1)
dayfield.options[today.getDate()] = new Option(today.getDate(), today.getDate(), true, true) //select today's day
for (var m = 0; m < 12; m++)
    monthfield.options[m] = new Option(monthtext[m], monthtext[m])
monthfield.options[today.getMonth()] = new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
var thisyear = today.getFullYear();
for (var y = 0; y < 100; y++) {
    yearfield.options[y] = new Option(thisyear, thisyear)
    thisyear -= 1
}
yearfield.options[0] = new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year

} }

You can try reverse For loop.您可以尝试反向 For 循环。

    for (var y = 100; y >=1; y--) {
    yearfield.options[y] = new Option(thisyear, thisyear)
    thisyear += 1
    }

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

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