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?
Printable View
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?
Not sure what DateDiff and formatting have in common in your question but here we go:
VB Code:
Debug.Print Format(Now, "mm/dd/yyyy") 'both dates must be defined as Date or at least have recognizable dates format as shown above Debug.Print DateDiff("d", Date1, Date2)
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...
You have to use # to assign:
About your 01/00/00: day cannot be 0! :)VB Code:
Dim VarName As Date VarName = #1/1/2000#
Umm... then I need to subtract one month from a date... how would I do that???
Quote:
Originally Posted by khanjan_a2k
VB Code:
Dim VarDate As Date VarDate = DateAdd("m", -1, Date) 'Substract 1 month from current date
Just want to make sure... so...
Is this the correct way to do it...
VB Code:
Dim datToday As Date Dim datNew As Date datToday = Format(Now, "mm/dd/yyyy") datNew = DateAdd("m", -1, datToday)
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.VB Code:
datToday = Now
Oh thanx for your help jcis... I really appreciate it
Khanjan
Well, that wasn't clear and in fact it appeared as it was a typo so try not to name your variables similar to those VB functions...Quote:
Originally Posted by khanjan_a2k
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.