locked
Number of days between 2 dates RRS feed

  • Question

  • Hi friends.

    I have your help. Again.

    I want to make little program to calculate number of days between 2 dates. Something like this - with two DateTimePicker, one TextBox and one Button:

    What code to put in button to calculate number of days between dates and show it in textbox ?

    If you have another idea without DateTimePicker ... with two Month Calendars, pleas tell me.

    With best regards.

    vesodimov


    From Bulgaria

    • Moved by Tina-Shi Tuesday, July 14, 2015 5:49 AM related to this windows form
    Monday, July 13, 2015 2:12 PM

Answers

  • hello ,

    this looks like a homework i will not paste a working form.

    since you are using visual basic here you have a link to datediff function in VB:

    DateDiff Function (Visual Basic)


    Hope it helps. Spiri

    • Marked as answer by Youjun Tang Monday, July 20, 2015 9:48 AM
    Tuesday, July 14, 2015 5:13 AM

All replies


  • (Date1- Date2).TotalDays

    more explicit

    DateTime d1 = this.dateTimePicker1.Value;
                DateTime d2 = this.dateTimePicker2.Value;
                MessageBox.Show(((int)(d1 -d2).TotalDays).ToString());
    
                TimeSpan t = d1-d2;
                MessageBox.Show(t.ToString());


    Hope it helps. Spiri


    • Edited by Spiri91 Monday, July 13, 2015 2:27 PM
    Monday, July 13, 2015 2:20 PM
  • Thank you, Spiri.

    First I got more than 20 errors. When I edit your code I have just one. In line

    MessageBox.Show(((Int())(d1() - d2()).TotalDays).ToString())


    I got this message :

    Argument not specified for parameter 'p1' of 'Private Function Int(p1 As Object) As Object'.
    

    Where is my mistake ?

    Now all my code is like this:

    DateTime(d1() = this.DateTimePicker1.Value)
            DateTime(d2() = this.DateTimePicker2.Value)
            MessageBox.Show(((Int())(d1() - d2()).TotalDays).ToString())
    
            TimeSpan(t() = d1() - d2())
            MessageBox.Show(t.ToString())

    Q2: where to put the code ? In TextBox or in Button ?

    Regards

    vesodimov



    From Bulgaria

    Monday, July 13, 2015 5:01 PM
  • hello ...first. when do you want this to be fired?

    if in the button click then write it in the button click event

    first you have to declare the variables :

                DateTime d1 = this.dateTimePicker1.Value;
                DateTime d2 = this.dateTimePicker2.Value;

    make sure you type the wright names for the Datetimepicker

    this is one method ...

     MessageBox.Show(((Int())(d1() - d2()).TotalDays).ToString())
    this is another method..
            TimeSpan(t() = d1() - d2())
            MessageBox.Show(t.ToString())


    Hope it helps. Spiri

    Monday, July 13, 2015 6:00 PM
  • Hi.

    Two methods not work for me. Please send me screenshot on your working Form. My result is:


    From Bulgaria

    Monday, July 13, 2015 6:52 PM
  • hello ,

    this looks like a homework i will not paste a working form.

    since you are using visual basic here you have a link to datediff function in VB:

    DateDiff Function (Visual Basic)


    Hope it helps. Spiri

    • Marked as answer by Youjun Tang Monday, July 20, 2015 9:48 AM
    Tuesday, July 14, 2015 5:13 AM
  • I find the correct code :

    TextBox1.Text = (DateTimePicker2.Value - DateTimePicker1.Value).Days


    From Bulgaria

    Thursday, August 20, 2015 9:52 AM