Page 1 of 3 123 LastLast
Results 1 to 40 of 82

Thread: ***resovled****I need urgent help with a date calculator

  1. #1

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109

    Exclamation ***resovled****I need urgent help with a date calculator


    I'm relativly new to Vb i juststarted like 2 weeks ago. I have a project to complete and i want VB to tell me the number of days that have passed between date given by user and today's date i believe i got most of the coding right but plz help me i need this done for tomarrow....here is my code

    Option Explicit
    Dim d1 As Long
    Dim m1 As Long
    Dim y1 As Long

    Private Sub cmdcompute_Click()
    picfee.Cls
    Dim Name As String
    Dim Book As String
    Dim CheckMonth As String
    Dim CheckDay As String
    Dim CheckYear As String
    Dim m2 As Long
    Dim d2 As Long
    Dim y2 As Long
    Dim Totaldays As Long
    Dim i As Long

    Totaldays = (y2 - y1) * 365
    ' 30 day months: september 9 april 4 june 6 november 11
    ' 28 day months: february

    For i = m1 To m2 - 1
    If i = 4 Or i = 6 Or i = 9 Or i = 11 Then
    Totaldays = Totaldays + 30
    Else
    If i = 2 Then
    Totaldays = Totaldays + 28
    Else
    Totaldays = Totaldays + 31
    End If
    Next i


    Name = txtname.Text
    Book = txtbook.Text
    CheckMonth = txtcheckmonth.Text
    CheckDay = txtcheckday.Text
    CheckYear = txtcheckyear.Text

    picfee.Print Name; " now owes for the book " & Book; " that was was checked out on " & CheckMonth; "-" & CheckDay; "-" & CheckYear

    End Sub

    Private Sub Form_Load()
    lbldate.Caption = Format(Now, "dddd, mmmm, dd, yyyy")
    m1 = Month(Now)
    d1 = Day(Now)
    y1 = Year(Now)
    txttodaymonth.Text = m1
    txttodayday.Text = d1
    txttodayyear.Text = y1
    End Sub

    Private Sub Timer1_Timer()
    If lbltime.Caption <> CStr(Time) Then
    lbltime.Caption = Time
    End If
    End Sub
    Last edited by Supreme Cookie; Nov 9th, 2002 at 02:21 PM.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Difference between two dates:

    VB Code:
    1. Private Sub Form_Load()
    2. Dim dDate As Date
    3.  
    4.   dDate = "11/28/02"
    5.  
    6.   MsgBox DateDiff("s", today, dDate)
    7. End Sub

    Gives you the number of seconds of difference.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This is a function I wrote awhile ago:

    VB Code:
    1. Public Function GetRemaining(ByVal i As Long) As String
    2. Dim strString As String
    3.  
    4.   'get number of days:
    5.   If Abs(i \ 86400) <> 0 Then
    6.     strString = Abs(i \ 86400) & " days, "
    7.   End If
    8.   i = i Mod 86400
    9.  
    10.   'get number of hours:
    11.   If Abs(i \ 3600) <> 0 Then
    12.     strString = strString & Abs(i \ 3600) & " hours, "
    13.   End If
    14.   i = i Mod 3600
    15.  
    16.   'get number of minutes:
    17.   If Abs(i \ 60) <> 0 Then
    18.     strString = strString & Abs(i \ 60) & " minutes "
    19.   End If
    20.   i = i Mod 60
    21.  
    22.   'get number of seconds:
    23.   strString = strString & i & " seconds"
    24.  
    25.   GetRemaining = strString
    26. End Function

    Just use the DateDiff() function to get the number of seconds, then pass it to the function to get a formatted string with days, minutes and seconds.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109

    Unhappy

    um... i don't think you quite got what i'm trying to do. check the attached file and see what i'm trying to do. Attached is the project and frm. PLZ HELP if you can figure out the code just paste it in and post back.
    Attached Files Attached Files

  5. #5

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    the time is irrelivant i just need the difference between two dates like today and 11/23/1976

  6. #6
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299
    Use the DateDiff function. It doesn't need to return seconds it can return days, weeks months years and the like.

    Look it up in the Help. Or type in the name, move over it and hit F1.

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Supreme Cookie
    the time is irrelivant i just need the difference between two dates like today and 11/23/1976
    Then do this:

    VB Code:
    1. Private Sub Form_Load()
    2. Dim dDate As Date
    3.  
    4.   dDate = "11/28/02"
    5.  
    6.   MsgBox DateDiff("d", Now, dDate)
    7. End Sub
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Supreme Cookie
    um... i don't think you quite got what i'm trying to do. check the attached file and see what i'm trying to do. Attached is the project and frm. PLZ HELP if you can figure out the code just paste it in and post back.
    No one's going to do your homework for you. We give you code, you find out how to incorporate it.

    If someone reposts your code but completely working, I'm going to be horribly disappointed that they'd do such a thing.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299
    Originally posted by The Hobo
    If someone reposts your code but completely working, I'm going to be horribly disappointed that they'd do such a thing.
    agreed.

  10. #10

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    your right, i'm sorry for asking thank you for your help so far.

  11. #11

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    but does this method account for leap years and the whole some months have 30 days others have 31 and fabruary has 28?

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Supreme Cookie
    your right, i'm sorry for asking thank you for your help so far.
    Give it a go and if you're having problems, we'd be glad to help.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Supreme Cookie
    but does this method account for leap years and the whole some months have 30 days others have 31 and fabruary has 28?
    You betcha. It's all built in there.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  14. #14

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    now that's just plain fancy....

  15. #15
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299
    Originally posted by Supreme Cookie
    now that's just plain fancy....
    Easy huh?

    saves a ton of your code too

  16. #16
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    And it cuts out a lot of code too.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  17. #17

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    hmmm....i'm doing something wrong but i'm not sure why it's won't let me use variables here where it days ddate = m2/d2/y2

    sorry if I sound like a real n00b but that's what i am

  18. #18

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    Option Explicit
    Dim d1 As Long
    Dim m1 As Long
    Dim y1 As Long
    Private Sub cmdcompute_Click()
    picfee.Cls
    Dim Name As String
    Dim Book As String
    Dim CheckMonth As String
    Dim CheckDay As String
    Dim CheckYear As String
    Dim m2 As Long
    Dim d2 As Long
    Dim y2 As Long
    Dim dDate As Date
    txtduemonth.Text = m2
    txtdueday.Text = d2
    txtdueyear.Text = y2
    dDate = m2 / d2 / y2 <---see there i think is where i'm messing up

    MsgBox DateDiff("d", Now, dDate)


    Name = txtname.Text
    Book = txtbook.Text
    CheckMonth = txtcheckmonth.Text
    CheckDay = txtcheckday.Text
    CheckYear = txtcheckyear.Text

    picfee.Print Name; " now owes for the book" & Book; " that was checked out on" & CheckMonth; " - " & CheckDay; " - " & CheckYear; ""

    End Sub

    Private Sub Form_Load()
    lbldate.Caption = Format(Now, "dddd, mmmm, dd, yyyy")
    m1 = Month(Now)
    d1 = Day(Now)
    y1 = Year(Now)
    txttodaymonth.Text = m1
    txttodayday.Text = d1
    txttodayyear.Text = y1

    End Sub


    Private Sub Timer1_Timer()
    If lbltime.Caption <> CStr(Time) Then
    lbltime.Caption = Time
    End If
    End Sub

  19. #19
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    pretty close. Since \ is part of a string, you need to surround it with quotes:

    VB Code:
    1. dDate = m2 & "/" & d2 & "/" & y2
    My evil laugh has a squeak in it.

    kristopherwilson.com

  20. #20

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    ahh good call thx

  21. #21
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    and the & sign is the concea...tation character. It adds parts of strings together. (sorry, I can never remember that 'c' word)

    But:

    VB Code:
    1. Msgbox "Dog" & " is " & "cool!"

    Will give you "Dog is cool!" in the MessageBox.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  22. #22
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299
    You see how you're formatting the lblCaption date, do the same with your d2-m2-y2 thingame when you put it into the date variable.

    eg
    Code:
        dim x as date
        x = format(days1 & "-" & months1 & "-" & years1, "dd-mmm-yyyy")
    oh and the word is concatenation... (kon-cat-in-ay-shon)
    (ok pick the dude that was a human dictionary in school)

  23. #23

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    hmmmm.....it keeps wanting an end of statement after i formated it like you have it there i even tried semicolons and no dice it keeps highlighting the "/"

  24. #24

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    ok cool let me try it

  25. #25
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299
    Oh you may want to format it the way you use it over there
    eg
    mm-dd-yyyy
    instead of
    dd-mm-yyyy


  26. #26

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    for the date diff what is the interval as string thing??

  27. #27
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Supreme Cookie
    for the date diff what is the interval as string thing??
    If you want days, it's "d" with quotes around it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  28. #28
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299
    The Help file has all the variations for that... look in there for more

  29. #29

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    ah ok i gotcha now should i dim the m2, y2 and d2 as date or shall i leave them as long?

  30. #30
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299
    leave em long

  31. #31

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    otay i left em long *snickers* but now i'm getting a type mismatch error on the dDate = line

  32. #32
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You could leave them as long, but I'd put them as integer, since they won't be even close to 32676. And integers only use 16 bytes of memory, rather than 32.

    But it doesn't really matter
    My evil laugh has a squeak in it.

    kristopherwilson.com

  33. #33
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Supreme Cookie
    otay i left em long *snickers* but now i'm getting a type mismatch error on the dDate = line
    What's your exact line?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  34. #34

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    ok but i still don't understand the error i'm getting

  35. #35
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299
    what does your "line" equal?

  36. #36

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    i get a type mismatch on this line:

    dDate = Format(m2 & "-" & d2 & "-" & y2, "mm-dd-yyyy")

  37. #37
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    299
    damn i'm too slow at this

  38. #38

    Thread Starter
    Lively Member Supreme Cookie's Avatar
    Join Date
    Nov 2002
    Location
    A small box
    Posts
    109
    lol

  39. #39
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Supreme Cookie
    i get a type mismatch on this line:

    dDate = Format(m2 & "-" & d2 & "-" & y2, "mm-dd-yyyy")
    If you're adding the hypens in manually (which you are), then you don't need format.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  40. #40
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    If you just do this:

    VB Code:
    1. dDate = m2 & "-" & d2 & "-" & y2

    Do you get an error?

    And all three of those variables are declared Long?
    My evil laugh has a squeak in it.

    kristopherwilson.com

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width