How to get the differences within a range of dates

Hey whats up experts. I have a simple question I want to know how to subtract or use the DateDiff function to get a particular value from anydate.

Example let say If the user chooses from a datetime picker May 26, 2006 . I want to know how can i compare the system date to the actual date
that the user has chosen and give me how many days apart is it from each other.

So if the user takes May 26 and today is June 2
I want to be able to display the differences in days on another text box which in this case would be " 7"  if you minus today date with the one that was chosen.

Well I try to do this on my own and I know I'm a bit close to it but my futile attempts have gone no where lol

Here is a sample code
///////////////////////////////////////////////////////////
 Dim StorePDate As Date
        StorePDate = PurchaseDate.Value  'picked by the User '
       
        StoreDate(StorePDate) 'Sends the value to a method


    End Sub
   
    Public Sub StoreDate(ByVal PurchaseD)

         PurchaseD = CInt(DateDiff(DateInterval.Day - ?????  )) '''Here I try to use System.DateTime...but it gives me an error

        txtDaysIN.Text = PurchaseD
end sub

/////////////////////////////

So if anyone can please help me with this little issue I would gladly appreciate it..also please let me know where did I go wrong..or was I close at all lol.

Take care experts ^_^



masterat03Asked:
Who is Participating?
 
AlexFMCommented:
You can use DateTime.Subtract Method which returns TimeSpan. TimeSpan.Days Property gives number of days that you need.
0
 
MageDribbleCommented:
you need to change StoreDate to a function.  A function returns a value while a sub (or sub-routine) doesn't.  So it should become:

Public Function StoreDate(ByVal PurchaseD as Date) as Date
 return DateDiff(d,PurchaseDate,Date.Now)
End Function

you would call it as:

txtDaysIN.Text = StoreDate(<Put your date in here>)
0
 
masterat03Author Commented:


In this example

Public Function StoreDate(ByVal PurchaseD as Date) as Date
 return DateDiff(d,PurchaseDate,Date.Now)
End Function


What is "d" suppose to be?... When i insert D it tells me to declare it....
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
MageDribbleCommented:
sorry - that is SQL Server syntax.

replace d with DateInterval.Day

it'll be:  return DateDiff(DateInterval.Day,PurchaseDate,Date.Now)
0
 
masterat03Author Commented:

lol understandable I know how it is when you jump from one environment to another.

Anyhow I tried this out, but I get an error telling me
return DateDiff(DateInterval.Day,PurchaseDate,Date.Now) ''''Value of type 'Long' cannot be converted to 'Date'. '''''
0
 
MageDribbleCommented:
sorry.  DateDiff returns a number not a date so the function needs to be changed to:

Public Function StoreDate(ByVal PurchaseD as Date) as Long
 return DateDiff(DateInterval.Day,PurchaseDate,Date.Now)
End Function

Sorry about that....it's almost 5:00 on a Friday  ;)
0
 
masterat03Author Commented:


hehe I understand I'm about 1hr away from 5pm so its kinda like rush coding before I leave my workplace.

Hey when I ran my program I got this error now
////Error Message
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Argument 'Date1' cannot be converted to type 'Date'.
////

so far this is how the code looks like

 Private Sub txtDaysIN_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDaysIN.TextChanged

        Dim StorePDate As Date
        StorePDate = PurchaseDate.Value
        'StoreDate(StorePDate)

        txtDaysIN.Text = StoreDate(StorePDate)

    End Sub

    Public Function StoreDate(ByVal PurchaseD) As Long


        Return DateDiff(DateInterval.Day, PurchaseDate, Date.Now)
            End Function

//////////////////////////////////////////


The error Occurs if I try to do anything to the textbox...Hence put a number or etc.

Now if I don't do anything well I don't get any output to the text box .
0
 
MageDribbleCommented:
it is trying to call the function once you type something.  So you type "5" and it runs the function.  It is trying to convert 5 to a date which it can not do.  You can add error handling to fix this.  

You should also put the code in the Lost_Focus event, this'll fire when the user is done typing and clicks outside the text box.
0
 
masterat03Author Commented:
I think we are missing our goal here.

First and foremost this is what I have. A user chooses a date from a dateTimePicker..and then on the txtDaysIN.text box
the output would display the differences from the current day which would be today minus the date chosen on the calender.

Now let say they pick a future date instead of the past then nothing should appear at the text box.

So i don't think Lost focus would do the thing...perhaps mouseEnters the text box or etc
0
 
masterat03Author Commented:

I'm sorry if i wasnt clear enough before, the only reason I said if the user puts in a number in the text box an error would occur is because I wasn't getting an output to my textbox so i decide to put a number to see what would happen.

Well anyhow nothing did happen like I said, but I was going to say if you notice the error message
///Error Message
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Argument 'Date1' cannot be converted to type 'Date'.
////

It seems as if it is try to convert it back to date format...which I really don't want it to because that is not my objective.
Is there another way to do this...before when I was trying I know i was wrong, but I was using a bit of math to Minus the system date with the user chosen date.

Also I do believe we are close to finding the answer I just wanted to know perhaps if we try to convert it to Integer value for the days before it Returns the value to the textbox that would probably work out..

 
0
 
MageDribbleCommented:
As I understand, there a text box being populated with the date and we want to populate a second text box (txtDaysIN) with the difference in days?  Am I incorrect?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
As AlexFM pointed out long ago...

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        TextBox1.Text = DateTime.Now.Subtract(DateTimePicker1.Value).Days
    End Sub
0
 
masterat03Author Commented:
LOL you are right Idle_Mind the answer was there and me and Magedribble went far into other things .

Well I'm sorry Idle_Mind for not addressing your answer right away...it was friday and things go on a rush for that day.

Anyhow thankyou guys for your help and you to magedribble.

0
 
masterat03Author Commented:
Opps I meant to say sorry to AlexFM...sorry got up from a long night just now lol
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol...  =)

Glad you got a working solution...now go take a nice long nap....ZZZZZZZZzzzzzzzzz......
0
 
MageDribbleCommented:
glad we were able to help.  I didn't know about the datetime.now.subtract functionality so this was beneficial to me as well :)

good luck with your project!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.