javascript date comparison

I have a date string format yyyy-mm-dd hh:mm:ss am/pm .

I wish to convert this into a javascript Date object and compare with current date time.

How do I do it ?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
just use Date.parse or new date():

and compare it to Date() value ...
Dave BaldwinFixer of ProblemsCommented:
I decided to figure this out myself so this is kind of a long demo.  Javascript doesn't do 'AM/PM' as far as I can tell and it is a little restrictive about the Date formats it accepts.  So I wrote this page that gets the current Date-Time as a number and compares 3 different dates to it after converting them to an acceptable format.  If the date is greater than today, the result is negative.  I left all my test displays in there so you could see where I was getting things.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


<p>From <a href=""></a></p>
<p id="cd0"></p>
<p id="cd1"></p>
<p id="cd11"></p>
<p id="cd2"></p>
<p id="cd21"></p>
<p id="cd3"></p>
<p id="cd31"></p>
<script type="text/javascript">
todate = new Date();
todate2 = todate.getTime();
document.getElementById("cd0").innerHTML = "Now = "+todate2;
// 1st date
chkdate1 = "2015-05-23 11:23:26 PM";
chkdate10 = chkdate1.substring(0,10); 
chkdate11 = chkdate1.substring(11,19); 
chkdate12 = chkdate1.substring(20,22); 
if(chkdate12 == 'PM') {
	chkdate13 = chkdate11.substring(0,2);
	chkdate14 = (chkdate13*1) + 12;
	chkdate15 = chkdate14.toString()+chkdate11.substring(2,8);
	document.getElementById("cd1").innerHTML = chkdate1+' | '+chkdate10+' | '+chkdate11+' | '+chkdate12+' | '+chkdate15;
	chkdate18 = chkdate10+'T'+chkdate15;
else {
	document.getElementById("cd1").innerHTML = chkdate1+' | '+chkdate10+' | '+chkdate11+' | '+chkdate12;
	chkdate18 = chkdate10+'T'+chkdate11;
chkdate19 = new Date(chkdate18);
chkdate199 = chkdate19.getTime();
chkdate1999 = todate2-chkdate199
document.getElementById("cd11").innerHTML = todate2+' - '+chkdate199+' = '+chkdate1999;

// 2nd date
chkdate2 = "2015-05-31 10:22:25 AM";
chkdate20 = chkdate2.substring(0,10); 
chkdate21 = chkdate2.substring(11,19); 
chkdate22 = chkdate2.substring(20,22); 
if(chkdate22 == 'PM') {
	chkdate23 = chkdate21.substring(0,2);
	chkdate24 = (chkdate23*1) + 12;
	chkdate25 = chkdate24.toString()+chkdate21.substring(2,8);
	document.getElementById("cd2").innerHTML = chkdate2+' | '+chkdate20+' | '+chkdate21+' | '+chkdate22+' | '+chkdate25;
	chkdate28 = chkdate20+'T'+chkdate25;
else {
	document.getElementById("cd2").innerHTML = chkdate2+' | '+chkdate20+' | '+chkdate21+' | '+chkdate22;
	chkdate28 = chkdate20+'T'+chkdate21;
chkdate29 = new Date(chkdate28);
chkdate299 = chkdate29.getTime();
chkdate2999 = todate2-chkdate299
document.getElementById("cd21").innerHTML = todate2+' - '+chkdate299+' = '+chkdate2999;

// 3rd date
chkdate3 = "2015-06-23 07:19:02 PM";
chkdate30 = chkdate3.substring(0,10); 
chkdate31 = chkdate3.substring(11,19); 
chkdate32 = chkdate3.substring(20,22); 
if(chkdate32 == 'PM') {
	chkdate33 = chkdate31.substring(0,2);
	chkdate34 = (chkdate33*1) + 12;
	chkdate35 = chkdate34.toString()+chkdate31.substring(2,8);
	document.getElementById("cd3").innerHTML = chkdate3+' | '+chkdate30+' | '+chkdate31+' | '+chkdate32+' | '+chkdate35;
	chkdate38 = chkdate30+'T'+chkdate35;
else {
	document.getElementById("cd3").innerHTML = chkdate3+' | '+chkdate30+' | '+chkdate31+' | '+chkdate32;
	chkdate38 = chkdate30+'T'+chkdate31;
chkdate39 = new Date(chkdate38);
chkdate399 = chkdate39.getTime();
chkdate3999 = todate2-chkdate399
document.getElementById("cd31").innerHTML = todate2+' - '+chkdate399+' = '+chkdate3999;

// -->


Open in new window

cofactorAuthor Commented:

I am worried about AM/PM stuff . I dont see any exmple which takes input in this format  

yyyy-mm-dd hh:mm:ss am/pm to form a Date()
Rainer JeschorCommented:
I would recommend the usage of the JS library moment.js

There you can pass the format string both for input/date creation as well as output.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cofactorAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.