Calculations between dates (DateTimePicker - DateDiff)

Today I want to show how to perform calculations between dates comfortable in visual basic. Net using the DateDiff function.

Need a form, button, some label's and a DateTimePicker control as shown in the image below:

and in the button click event will the following code:

 txtAños.Text = DateDiff (DateInterval.Year, DateTimePicker1.Value, Now.Date) & "years"
 txtMeses.Text = DateDiff (DateInterval.Month, DateTimePicker1.Value, Now.Date) & "months"
 txtSemanas.Text = DateDiff (DateInterval.Day, DateTimePicker1.Value, Now.Date) / 7 & "weeks"
 txtDias.Text = DateDiff (DateInterval.Day, DateTimePicker1.Value, Now.Date) & "days"

run the application, we selected a past date and press the calculate button. The result will be similar to this image:

This is a very simple example, just look at some code and screenshots and I'm sure you will not have doubts, but if you were to have any, just leave your comment.

Hope this helps.

18 Responses to "calculations between dates (DateTimePicker - DateDiff)"

  1. Information Bitacoras.com ...

    Rate in Bitacoras.com: Today I want to show how to perform calculations between dates in visual basic. Net using the DateDiff function. Need a form, button, some label? S DateTimePicker control, as shown in the fol ...

  2. Peter says:

    Hello first of all thank you.

    I have a concern.

    Today is April 29, 2009
    I was born in September 1980

    according
    txtAños.Text = DateDiff (DateInterval.Year, DateTimePicker1.Value, Now.Date) & "years"

    now I have 29 years

    but that is not so, I have 28 for my birthday recently in September

    and modify the code

    thanks again

    • Elisha says:

      Peter, I extend you a little information:

      If Interval is set to DateInterval.Year, the return value is simply calculated from the year parts of the two given dates. Similarly, the return value for DateInterval.Month is simply calculated from the parties of the year and month of the arguments.

      For example, when comparing December 31 to January 1 next year, DateDiff returns 1 for DateInterval.Year or DateInterval.Month, but only as much has passed a single day.

      In those cases I recommend, for example, always use the range and divide by 365 DateInterval.Day and so would have a more accurate figure: 28.65 years (for example) and if you just want the year you can use the int () function that will return only part of the number.

  3. Alex says:

    A good example! Thanks for the info

  4. jimmy says:

    Hey good post men who served me the truth so far is helping the camera as I grax.

  5. ignacio jose says:

    your post very good friend like you to set the DateTimePicker format DD / MM / YYYY I get by qa MI (Thursday, 12 May 1999) and really do not know how

    • Elisha says:

      Hello joseph:

      you just have to change the ownership Formatdel control as well:

      DateTimePicker1.Format = DateTimePickerFormat.Short

      Luck

  6. John F. says:

    A consultation is perhaps too easy, but I could change the way of displaying the date in the datetiempicker, to me looks like: Friday, November 27, 2009, I need simply: 27/11/2009.

    that would be, thanks.

  7. John F. says:

    Sorry, the comment had not seen before, which answers my question, thanks.

  8. mcinpapr says:

    Good afternoon ... I have a problem ... I'm using two DateTimePicker in vb.net 2005 and when I select a date in the second DateTimePicker should I take the difference between two dates ... The function is started at the "ValueChanged" the second ... If you run it but I always subtract one day than I should give ... for example ... If I leave today's date to the first DateTimePicker (21/12/2009) and selected in the second the next day (22/12 / 2009) returns me a value of 0 ... if you select him on 23 returns me the value of 1. The function used is as follows:

    TextBox1.Text = CStr (DateDiff (DateInterval.Day, DTP_ShipDate.Value, DTP_ETA.Value))

    Anyone know what may be what happens please?

  9. Lilimarlet says:

    hello, how are you?
    I have a query them, as I can get just the day of the FEHA, look I have a textbox where I show the feha "01/01/2009" and I need one day.

    I have this

    dim day as integer
    day = day (tfecha.text)

    I do not bring anything, but put

    Dim year As Integer
    year = year (tfecha.text)

    I shows the year here but I need the day

  10. Carlos h says:

    Excuse me as I make the dataTimerPicker I debuelva year-month-day bone "yyyy / mm / dd" to select a date, I need for a query in Mysql and dates in this format keeps them ("yyyy / mm / dd" ) .....

    If I could be the agradesco guide ... Thanks in advance.

  11. Jennifer says:

    Thank you very much for the information. It was a great help.

  12. hanner Oviedo says:

    Good management is not very good VB and I'm working this event but I get error 424 hit the little I have done, I hope your help

    Range ("I10") = DateDiff (DateInterval.Month, TextBox18.Value, Now.Date)

    Thank you.

  13. DjMiki says:

    Good Friend Elisha, thanks for your function served me, but I have a detail, the result gives me the DateDiff is a negative, follow the example you are giving but equal, try to pass it as string, int, cint, cstr, and nothing some help please.

  14. DjMiki says:

    Well Elisha excuse the trouble but good and solve my problem, but I think you placed the order parameters are the other way round, and I mean the date 1 and date 2, and then should be Date1 Date2.
    Well corrected, now struggling with comparison of this result.

  15. Elkin V says:

    Hey Friend Thank You Very Useful
    It's very easy lol

    A remnant of time later trying to find something

    Do I cast a big favor!

Comment