Help in Date on JavaScript ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Help in Date on JavaScript ?

hello, function myFunction() { var x = document.getElementById("myDate").value; var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1; //January is 0! var yyyy = today.getFullYear(); document.getElementById("demo").innerHTML = x; } I want to use the date entered and put it in the new date

27th Jun 2017, 7:18 AM
Mohammed Elhafed Messini
Mohammed Elhafed Messini - avatar
5 Answers
+ 10
use var today = new Date(x); if (today == 'Invalid Date') { ..... // handle wrong input ..... } else { .... // do your thing ..... }
27th Jun 2017, 7:32 AM
Nikolay Nachev
Nikolay Nachev - avatar
+ 8
@S Vengat depends how strict you want to be. As usual JS would try to be nice and evaluate anything you pass: -1.4.17 => Jan 04 2017 41.4.17 => Apr 17 2041, but 41.4.41 => Invalid Date, So like with anything else JS will do it's best to make sense of the input, but one has to be careful with that 😃😃😃
27th Jun 2017, 9:59 AM
Nikolay Nachev
Nikolay Nachev - avatar
+ 5
Don't you also need to note that they have their limits? Like 01<=dd<=31?
27th Jun 2017, 9:43 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
Oh okay! So that's why timers can set 99:99:99 and the timer will still run!
27th Jun 2017, 10:03 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
- 1
znjsnbsb@&#-~-~÷~(@&@-*~*|×@×|+₹^~&!×+#&#*@*@-#+|*[♀√¡¡8 il il sisiwjsh8wkakkazhjskghhsiwjsgsh
27th Jun 2017, 11:24 AM
Sk Vinod
Sk Vinod - avatar