Monday, 20 August 2012

JavaScript: Date Compare Function



JavaScript has very nice build-in functions. As per date function, it should have more function like date diff and date compare. When C# has variety of date function, JavaScript contains very limited date function. For example, registration form contains DOB and Wedding day. We need to check the condition that DOB should not be greater than Wedding day.
In C# and other programing languages, we can check date compare like this.
int result = DateTime.Compare(date1, date2);
 
But in JavaScript, we need to write our own function to compare two dates. When I encounter this issue, I wrote a function like this.
 
<head>
<html>
<scrip>
 
function addZero(n) {
return n < 10 ? '0' + n : '' + n;
}
function compare(){
var currentDate = new Date() + 20;
     
var dt = document.getElementById("txtDob").value.split("/");
var firstDate = dt[2] + addZero(dt[1]) + addZero(dt[0]);
              
dt = document.getElementById("txtWedding").value.split("/");
var secondDate = dt[2] + addZero(dt[1]) + addZero(dt[0]);
             
if (eval(firstDate) > eval(secondDate)) {
            alert("DOB should not be greater than Wedding date");
            return false;
       
}
   
return true;
}
</script>
</head>
 
 
<body>
<input type="text" id="txtDob" value="12/10/1985"/>
<input type="text" id="txtWedding" value="3/7/2010"/>
<input type="button" id="btnResult" value="Result" onclick="compare()" />
</html>
</body>
First, let see what is the logic behind this function.  My logic is that, we need to split the dates into date, month and year and concatenate them as year + month + date. So, we get DOB as 19851012, Wedding day as 20100703. Finally, we must check whether DOB (19851012) is greater than Wedding day (20100703). If greater function will return false.
Let me explain the function line by line.
var dt = document.getElementById("txtDob").value.split("/");
Here, we get DOB date and split by “/”, so, dt contains 12, 10, 1985 as array elements.
var firstDate = dt[2] + addZero(dt[1]) + addZero(dt[0]);
In this statement, we concate year and month and date and assign to firstDate which will have 19851012. secondDate also uses same logic to get concatenated date parts and has 20100703.
You might have noticed another function addZero which adds 0 before date part if it is single digit. If we do not use this function, date compare will give wrong result. For example, if Wedding day is 3/7/2010, concatenated value will be 201073 which is lesser than 19851012 and return false.
I will be happy if this function will be useful to at least one person. If you have any question or suggestion don’t hesitate to comment.
Thank you.
 

Medical

  1. Patients on long term medications should discuss with their obstetrician and concerned specialist ex. Obstetrician, Diabetics.
  2. Patients failing awtely ill should not take over the counter medications without proper advice.
  3. Avoid C.T., X-ray during pregnancy.

Food

  1. Patients should take food rich in folic acid, Iron and Calcium such as greens, vegetables, Fish , Non-Vegetarian diet.
  2. Avoid very spicy diet as it may aggravate vomiting, refluxes during pregnancy. Plenty of water should be taken.
  3. Ensure adequate bladder emptying as pregnant women are more prone for urinary tract infection.

Activity

  1. Patients should take food rich in folic acid, Iron and Calcium such as greens, vegetables, Fish , Non-Vegetarian diet.
  2. Avoid strenuous activities like weight caring.
  3. Avoid sexual activity during 1st trimester and during last 4 weeks or else in certain condition like placenta preavia, preterm pain.
This medical advice given by:
Dr. Jayanthi MBBS.,
Medical Consultant.

Medical Disclaimer
We would like to insist that TipsUpdate.blogspot gives you only information not solution. It is strongly suggested that please take medical counsel with doctors for any kind of problem. TipsUpdate.blogspot or Doctors will not be held responsible for any adverse implications or consequences. We request you to exit the website immediately if the Medical Disclaimer is not acceptable to you.
Share:

0 comments:

Post a Comment

Powered by Blogger.

Recent Posts

Unordered List

Pages

Theme Support