1

I have a javascript file i'm trying to use and the dates are not working out. Sorry if this a duplicate question. FYI - I'm not well versed in JS yet so please be patient!

To get current date I did this

var today = new Date()
var month = today.getMonth()+1;
var day = today.getDate();
var year = today.getFullYear();
var currentdate = month + "/" + day + "/" + year;

Now when I run this if statement it is telling me currentdate > week3 is true. What am I missing here?

var week3 = "04/17/2016";
var nl = "<br>";
if(currentdate > week3) {
document.write(week3.strike() + " " + week3info.strike() + nl); }
else {
document.write(week3 + week3info + nl); }
Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
  • 1
    week3 and currentdate are both strings so not comparable (it is but not the way you think). You should compare the timestamp from the date object eg. `if( new Date().getTime() > new Date( week3 ).getTime() ) { ... }` – synthet1c Dec 30 '15 at 21:11
  • @synthet1c Worked! Thanks for the help. Makes sense about the string issue. Thank again, big help :) – Nelsons Development Solutions Dec 30 '15 at 21:22

1 Answers1

2

currentdate and week3 are both strings, and are compared using a "fancy alphabetical order" when using > and <.

As stated in one of the comments, use Dates instead of strings for the type of comparison that you want.

if(new Date() > new Date(week3)) { ... }

See this SO post for more information if you're interested.

Community
  • 1
  • 1
Frank Bryce
  • 8,076
  • 4
  • 38
  • 56