DateSerial question

G

Guest

Can someone please explain to me why the following gives me the last day of
the previous month?

DateSerial(Year(Date()),Month(Date()),0)

I can see the logic of everything except the day part. How is it that the
",0)" gives the last day of the previous month?

curious,
 
G

Guest

My theory would be that the first two parameters would generate the date
serial for the 1st day of the required month to which it would add the third
parameter less one, which in this case would give you the last day of the
previous month.

Hope This Helps
Gerald Stanley MCSD
 
G

Guest

... add the third parameter less one, which in this case would give you the
last
day of the previous month.

Not following you on this one. How does a zero (0) in the last parameter,
give the last day of previous month? Sorry, but I just don't see it.

JMorrell
 
R

Rick Brandt

JMorrell said:
the

Not following you on this one. How does a zero (0) in the last parameter,
give the last day of previous month? Sorry, but I just don't see it.

Simple. The zeroth of a month is equal to the last day of the previous
month. It is one day before the first of the month right?

DateSerial is fully aware of all wrap points in the calendar. You can use
numbers that are too large (the 100th of a month for example) and you can
even use negative numbers for any of the arguments and DateSerial will
properly figure out what the correct date to return is factoring in leap
years and everything.

It really is the single-most versatile date function I've ever run across.
How I wish a few of the other platforms I work with had a similar one.
 
J

John Spencer (MVP)

DateSerial(Year(Date()),Month(Date()),1)
Is the first day of the month

DateSerial(Year(Date()),Month(Date()),1) - 1
is one day before the first of the month and that can be expressed as
DateSerial(Year(Date()),Month(Date()),1-1)
which evaluates to
DateSerial(Year(Date()),Month(Date()),0)

So thinking of it in the above manner may make it seem a bit more rational
 
G

Guest

I didn't think of the "zeroth" of the month as an actual date. It makes
sense now. It also makes more sense to me now when I see numbers within the
other parameters.

Thanks to you and John Spencer for the enlightenment!

JMorrell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top