Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Calculating the differece between two dates is one month in javascript using asp.net

Aug 12, 2010
Hi i have written one javascript function to find the two dates difference is one month

i am having two textboxes startdate and enddate. Now i want to write clientside javascript function to check the dates difference is one month or not if the user enter more than one month date range i want to raise error message.i have written it in server side but i want to write clientside javascript function to check dates range. i have searched for this many sites i have found the solution but they are finding the difference between two dates i.e number of days and they are taking by default 30 days difference for one month .Suppose user enter 2/1/2010(Feb/01/2010) and 3/3/2010(March/03/2010) how can we calculate in February we have only 28 days if we allow user up to 30 days means that not right way . So i have already written server side function for validating textboxes for date range based on that server side function i have implemented client side function (i.e.,javascript function)


 By using this javascript function we can find the two dates difference is one month or not

 <script language="javascript" type="text/javascript">

function DateDiffownfunction(sender, args)
 {
var dt1 = document.getElementById('tbStartDate').value; //frmdate-->MM/dd/yyyy Format
var dt2 = document.getElementById('tbEndDate').value; //todate-->MM/dd/yyyy Format;
//By using this expression we can change our date format into this expression format for getting number of days,months,years
var dateRegEx = /^([01]?\d)[\.\-\/\s]?([0123]?\d)[\.\-\/\s]?(\d{4})$/;
var result1 = dt1.match(dateRegEx);
var result2 = dt2.match(dateRegEx);
if (result1 != null) {
var month1 = result1[1];
var day1 = result1[2];
var year1 = result1[3];
}
if (result2 != null) {
var month2 = result2[1];
var day2 = result2[2];
var year2 = result2[3];
}
if (result1 && result2) {
var dif = 0;
var yeardif = 0;
dif += (month2 - month1);
yeardif += (year2 - year1);
}
if (dif == 0 && yeardif == 0) {
args.IsValid = true;
}
else if (yeardif == 0 && dif == 1) {
if (day1 >= day2) {
args.IsValid = true;
}
else {
args.IsValid = false;
}
}
else {
args.IsValid = false;
}
}
</script>
If you want Sample code 

Download sample code attached
 




If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

sharanya gona said...

plz give html code also

Suresh said...

hi sharanya i have attach sample here please chek it once and check this link also may be your requirement like this also
http://aspdotnet-suresh.blogspot.com/2010/08/calculating-difference-between-two.html

Subhash Jakhar said...

Hi Suresh
Can we Show the Current date time in div in html page using Java script.I need plz help me.
Thank You
Subhash

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.