Results 1 to 11 of 11

Thread: [RESOLVED] Date Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Resolved [RESOLVED] Date Question

    I have a small question... If I want to assign a date to a date variable in the code... How do i do it...
    I want the variable datDiff to be equal to 01/00/00 (mm/dd/yyyy)
    What format does the date have to be in?
    Hey... If you found this post helpful please rate it.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Date Question

    Not sure what DateDiff and formatting have in common in your question but here we go:
    VB Code:
    1. Debug.Print Format(Now, "mm/dd/yyyy")
    2.  
    3. 'both dates must be defined as Date or at least have recognizable dates format as shown above
    4. Debug.Print DateDiff("d", Date1, Date2)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Date Question

    Nooo... lol... Its datDiff not DateDiff... datDiff is a variable... and format is just like in what way can I assign a date to a date variable...

    Like I want to subtract a date (01/00/0000 - mm/dd/yyyy) from the actual date to get one month less... but I don't know how to assign 01/00/0000 to a variable... so what is the standard way of putting in a date during coding...

    I hope that makes it clear...
    Hey... If you found this post helpful please rate it.

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Date Question

    You have to use # to assign:
    VB Code:
    1. Dim VarName  As Date
    2.     VarName = #1/1/2000#
    About your 01/00/00: day cannot be 0!

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Date Question

    Umm... then I need to subtract one month from a date... how would I do that???
    Hey... If you found this post helpful please rate it.

  6. #6
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Date Question

    Quote Originally Posted by khanjan_a2k
    Umm... then I need to subtract one month from a date... how would I do that???
    VB Code:
    1. Dim VarDate  As Date
    2.     VarDate = DateAdd("m", -1, Date) 'Substract 1 month from current date

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Date Question

    Just want to make sure... so...
    Is this the correct way to do it...

    VB Code:
    1. Dim datToday As Date
    2. Dim datNew As Date
    3.  
    4. datToday = Format(Now, "mm/dd/yyyy")
    5. datNew = DateAdd("m", -1, datToday)
    Hey... If you found this post helpful please rate it.

  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Date Question

    VB Code:
    1. datToday = Now
    You don't need to use Format, a date is always a date. Format must be used just when showing a date to the user.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Date Question

    Oh thanx for your help jcis... I really appreciate it

    Khanjan
    Hey... If you found this post helpful please rate it.

  10. #10

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Date Question

    I would suggest making it a habit to use DateSerial() to assign the date whenever possible rather than passing a string... If passing a string, best to pass the month as MMM so there's no accidental misinterpretation (usually swapping of day and month digits) from having a different date format in regional settings.

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