How to code date format? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to code date format?

That has respective 30-31days each month and also does have 28days and 29th if leapyear(February) in javascript. https://code.sololearn.com/Wy3u5sH6LDa1/?ref=app Please help me!

25th Sep 2020, 2:53 AM
Carlo Jade
Carlo Jade - avatar
5 Answers
+ 2
This would work: <!DOCTYPE html> <html> <head> <title>JS</title> <script type="text/javascript"> // <![CDATA[ function myOnLoadFunction() { var opt = ""; for(var num = 1900; num <= 2020; num++) { opt += "<option>" + num + "</option>"; } document.getElementById("year").innerHTML = opt; // set values 1, 2... 12 on the month options. var monthOptions = document.getElementById('month').querySelectorAll('option'); monthOptions.forEach(function(monthOption, index) { monthOption.value = index + 1; }); updateNumberOfDaysInMonth(); } function updateMonthDays(numDays) { var daysSelect = document.getElementById('days'); var innerHTML = ''; for (var i = 1; i <= numDays; i++) { innerHTML += '<option>' + i + '</option>'; } daysSelect.innerHTML = innerHTML; } function updateNumberOfDaysInMonth() { var mon = document.getElementById("month").value; var year = document.getElementById('year').value; // Calculate the number of days in the month. // Adapted from: https://www.w3resource.com/javascript-exercises/javascript-date-exercise-3.php var numDays = new Date(year, mon, 0).getDate(); updateMonthDays(numDays); } // ]]> </script> </head> <body onload="myOnLoadFunction()"> <select id="month" onclick="updateNumberOfDaysInMonth()"> <option>January</option> <option>February</option> <option>March</option> <option>April</option> <option>May</option> <option>June</option> <option>July</option> <option>August</option> <option>September</option> <option>October</option> <option>November</option> <option>December</option> </select> <select id="days"></select> <select id="year" onclick="updateNumberOfDaysInMonth()"></select> </body> </html>
25th Sep 2020, 6:07 AM
Josh Greig
Josh Greig - avatar
+ 2
You can directly use <input type="date"> other wise if you want in your style refer this link https://code.sololearn.com/WHW0Cyhi4g0h
25th Sep 2020, 7:15 AM
Padala Vamsi
Padala Vamsi - avatar
+ 1
Carlo, did you actually try my code or just assume it looked too easy? This returns 29 for February 2020 because 2020 is a leap year: new Date(2020, 2, 0).getDate(); This returns 29 for February 1904 because 1904 is a leap year: new Date(1904, 2, 0).getDate(); This returns 28 for February 2019 because 2019 is not a leap year: new Date(2019, 2, 0).getDate(); This returns 31 for January 2020: new Date(2020, 1, 0).getDate(); My code works. If the requirement is that you need a longer implementation that is more error-prone than what is already done in the standard Date class, let me know.
25th Sep 2020, 3:45 PM
Josh Greig
Josh Greig - avatar
0
Josh Greig no leapyear and supposed to be each months have 31th like January, March, May, July, August, October, December and 30th are April, June, September, November and last but not the least February should have 28 days and if every leapyear(1904,1908,1912,1916,1920) should have 29th.
25th Sep 2020, 8:18 AM
Carlo Jade
Carlo Jade - avatar
27th Sep 2020, 2:12 PM
Kelvin Paul
Kelvin Paul - avatar