is there a way to compare dates/times using javascript that are already coded to output like this: 08/29/2007 08:30 to 08/30/2007 10:00 Var CurrentDateTime output: "MM/DD/YYYY HH:MM" I am trying to find a way to calculate if the deadline for a project has passed, and have been stuck on the if statement, since you cant really compare variables that have slashes, spaces, and colons in them... or can you? Any help would be appreciated. thx
I guess my original question was if you could do math with a variable that output like this "08/29/2007 9:30" I am going to go back to that. Is there a way, if I have no control over how the variable is populated if I can edit its contents. such as: If I have a variable named currentDateTime and its output is "MM/DD/YYYY HH:MM" is there a way to separate out its values like this: "MM" = currentMonth "DD" = currentDay "YYYY" = currentYear "HH" = currentHour "MM" = currentMinute. The reason I dont have control over the currentDateTime script is because its a built in function, that calls for the server time.
No, you can't do math on that directly (to my knowledge). You will have to split out the month, date, year, etc. I found this page: http://www.mattkruse.com/javascript/date/ Click "source" at the top and look at the getDateFromFormat function. Or, you could also split the string yourself. Off the top of my head: // Split by the space var arr = CurrentDateTime.split(" "); // arr[0] now has MM/DD/YYYY and arr[1] has HH:MM var arr2 = arr[0].split("/"); // arr2[0] is MM, arr2[1] is DD and arr2[2] is YYYY arr3 = arr[1].split(":"); // arr3[0] is HH and arr3[1] is MM Code (markup): Then you can create a javascript date object Look at the constructor for new Date(dateString) http://www.comptechdoc.org/independent/web/cgi/javamanual/javadate.html I don't know if that all made sense, let me know if you need more help.
i'd personally convert the times into unix time and then you can do a simple comparison this site converts to and from unix time, you can see how they do it http://www.captain.at/review-unixtime-javascript.php
But I think the way ssanders82 suggested, provides you good control. I would prefer to go that one, as I was also facing the same problem, let me send you once it is done.