cofactor
asked on
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 ?
I wish to convert this into a javascript Date object and compare with current date time.
How do I do it ?
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"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<p>From <a href="http://www.w3schools.com/js/js_date_formats.asp">http://www.w3schools.com/js/js_date_formats.asp</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;
// -->
</script>
</body>
</html>
ASKER
@Hengel,
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()
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()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ex
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
and compare it to Date() value ...