Click here to Skip to main content
15,884,648 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am devloping application in vb.net where i am using datepicker and want to restrict user not to select date which is less than 3 month from current date..not in terms of 90 days..in terms of month..
DateDiff(DateInterval.Month, dtpChequeDate.Value, Date.Now) > 3
but it gives me integer value i want float value so exactly month difference can calculated..
Posted
Comments
StianSandberg 19-Jul-12 6:21am    
A float??. What is 3,4 months?
vishal_h 19-Jul-12 7:12am    
I have added solution ..

You can write JavaScript method to calculate the number of months difference between two given dates as:

JavaScript
function monthDiff(d1, d2) {
    var months;
    months = (d2.getFullYear() - d1.getFullYear()) * 12;
    months -= d1.getMonth() + 1;
    months += d2.getMonth();
    return months;
}


Using this method, you can write your application logic as
monthDiff(dtpChequeDate.Value, Date.Now) > 3
 
Share this answer
 
Comments
StianSandberg 19-Jul-12 6:29am    
this is not an answer to vishal_h's question. This is just a javascript version of his c# code.
I write one function which calulate months diffenece and days difference ..
If month difference is 3 and days difference is greater than 0 then it is exactly more than 3 month..
Private Function IsDateValid(ByVal dtp As Date) As Boolean

       Dim d1, d2 As Date

       Dim days, months As Long
       d1 = dtp
       d2 = Now.ToShortDateString


           months = Month(d1)
           days = d1.Day


           months = Month(d2) - months
           days = d2.Day - days



           If Math.Sign(days) = -1 Then
               days = 30 - Math.Abs(days)
               months = months - 1
           End If

           If Math.Sign(months) = -1 Then
               months = 12 - Math.Abs(months)
           End If

           If ((months = 3 And days > 0) Or (months > 3)) Then
               Return False
           End If
           Return True
       End If

   End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900