HTML + JavaScript Date Setters funciton setting differnt date other than the input -
consider following code of html & javascript
<!doctype html> <html> <body> <script> var str = "20990229"; var showdate = new date(); showdate.setfullyear(str.substring(0, 4)) showdate.setmonth(parseint(str.substring(4, 6), 10) - 1) showdate.setdate(str.substring(6, 8)) document.write(showdate) </script> </body> </html>
output:
fri mar 01 2099 16:02:52 gmt+0530 (india standard time)
the output not correct one, going wrong not known.
could tell me going wrong?
the output should be:
var str = "20990229"; var showdate = new date(); showdate.setfullyear(str.substring(0, 4)); // set year 2099 showdate.setmonth(parseint(str.substring(4, 6), 10) - 1); // set month 1 showdate.setdate(str.substring(6, 8)); // set date 29
that 29th february 2099. (note months indexed 0).
since 2099 not leap year there no february 29th, , date corresponds march 1st.
if change year 1 leap year (such 2096) output expect. here's an example.
Comments
Post a Comment