Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nextday/Previousday

Status
Not open for further replies.

Kori

Programmer
Oct 17, 2000
35
US
Does anyone know a easy way to write some code that will find the next day and the previous day? I have a date but I want to know the day in advance and the day previous to that date. I figured this would be easy but it gets complicated when you are on the 31st or 30th or 1st...

 
Check out the DateSerial function.

MsgBox DateSerial(DatePart("yyyy", Now()), DatePart("m", Now()), DatePart("d", Now()))
' -1 for yesterday, +1 for tommorow

Mark
 
or datediff

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
ReturnedDate = DateSerial(DatePart("yyyy", year), DatePart("mm", month), DatePart("dd", day) - 1) is what I have typed in but I get a invalid call? ReturnDate is the variable I want to pass back too. Can anyone see something I am missing?
 
Thanks everyone I have it working now. I was being ignorant on my last post and figured it out.

Kori
 
'yesterday
MsgBox DateSerial(DatePart("yyyy", Now()), DatePart("m", Now()), DatePart("d", Now())) -1

'Tomorrow
DateSerial(DatePart("yyyy", Now()), DatePart("m", Now()), DatePart("d", Now())) +1

Mark
 
If you use the date part, it needs some date to get the part from. In the posted example given previously, htis was the "Now()". It is a REQUIRED arg for datepart.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Kori,

Just use the DateAdd function.
DateAdd(interval as String, number as Double, Date)

The interval specifies the period which you want added. This could be "m" for month or "d" for day in your case. Number is the amount of intervals that should be added (or subtracted). you have to use negative amounts to subtract. And the date is ofcourse the startingdate.

Also not that if you use only date and the hour is of no importance, you better use Date() instead of Now()

ex:
Tomorrow:
DateAdd("d", 1, Date())
Yesterday:
DateAdd("d", -1, Date())
6 hour ago (time is important so use now():
DateAdd("H", -6, Now())

Regard
Johpje
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top