5 Replies - 1924 Views - Last Post: 01 November 2012 - 09:49 AM Rate Topic: -----

#1 jhedonghae  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 29-March 12

adding date values from two datetimepicker

Posted 01 November 2012 - 09:27 AM

i want to add dates from two datetimepicker...for example the user chooses the date 1/1/2012 in the first picker and chooses 1/3/2012 in the second picker the answer should be 3 that will be shown in a textbox..
this is the codes that i tried:
 Dim dt1 As Date = Date.Parse(fromdatepicker.Text)
        Dim dt2 As Date = Date.Parse(todatepicker.Text)

        daysTB.Text = Math.Abs(CInt(dt2.Subtract(dt1).TotalDays))


that code gives me the answer of 2 which is not correct..please help me with this

Is This A Good Question/Topic? 0
  • +

Replies To: adding date values from two datetimepicker

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8948
  • View blog
  • Posts: 33,544
  • Joined: 12-June 08

Re: adding date values from two datetimepicker

Posted 01 November 2012 - 09:42 AM

why is two incorrect? It is correct!

If you need it to show an extra day then just add a day to your variable - before the subtraction.

dt2.AddDays(1).Subtract(dt1)

Was This Post Helpful? 0
  • +
  • -

#3 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: adding date values from two datetimepicker

Posted 01 November 2012 - 09:44 AM

why 3? 3-1 is 2
Was This Post Helpful? 0
  • +
  • -

#4 jhedonghae  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 29-March 12

Re: adding date values from two datetimepicker

Posted 01 November 2012 - 09:46 AM

View Postsela007, on 01 November 2012 - 09:44 AM, said:

why 3? 3-1 is 2

yes..but what i really want is to add those but i dont know how
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: adding date values from two datetimepicker

Posted 01 November 2012 - 09:47 AM

Why are you use converting the text of the datetime picker? It already returns an datetime object via .Value

Now if subtract one date from another to the about of time between them, you'll get a timespan object.
 Dim timeperiod As TimeSpan = DateTimePicker_EndDate.Value - DateTimePicker_StartDate.Value


This post has been edited by AdamSpeight2008: 01 November 2012 - 09:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 jhedonghae  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 29-March 12

Re: adding date values from two datetimepicker

Posted 01 November 2012 - 09:49 AM

View Postmodi123_1, on 01 November 2012 - 09:42 AM, said:

why is two incorrect? It is correct!

If you need it to show an extra day then just add a day to your variable - before the subtraction.

dt2.AddDays(1).Subtract(dt1)


tnx a lot this solve my problem:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1