Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to get the differences within a range of dates

Posted on 2006-06-02
16
Medium Priority
?
578 Views
Last Modified: 2012-05-05
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 ^_^



0
Comment
Question by:masterat03
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 600 total points
ID: 16819104
You can use DateTime.Subtract Method which returns TimeSpan. TimeSpan.Days Property gives number of days that you need.
0
 
LVL 5

Expert Comment

by:MageDribble
ID: 16819117
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
 

Author Comment

by:masterat03
ID: 16819308


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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Expert Comment

by:MageDribble
ID: 16819379
sorry - that is SQL Server syntax.

replace d with DateInterval.Day

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

Author Comment

by:masterat03
ID: 16819482

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
 
LVL 5

Expert Comment

by:MageDribble
ID: 16819558
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
 

Author Comment

by:masterat03
ID: 16819745


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
 
LVL 5

Expert Comment

by:MageDribble
ID: 16819796
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
 

Author Comment

by:masterat03
ID: 16821205
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
 

Author Comment

by:masterat03
ID: 16821241

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
 
LVL 5

Expert Comment

by:MageDribble
ID: 16821436
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16821956
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
 

Author Comment

by:masterat03
ID: 16823666
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
 

Author Comment

by:masterat03
ID: 16823678
Opps I meant to say sorry to AlexFM...sorry got up from a long night just now lol
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16823862
Lol...  =)

Glad you got a working solution...now go take a nice long nap....ZZZZZZZZzzzzzzzzz......
0
 
LVL 5

Expert Comment

by:MageDribble
ID: 16824028
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question