No of Days in a month (1 Viewer)

Jamaluddin Jamal

Registered User.
Local time
Today, 12:35
Joined
Apr 15, 2017
Messages
13
Hello all,

I am looking for a method to get No of Days (Count of Days) in a given month., like, if someone enter May 2017 in a first textbox (textbox is set to display Date as MMM YYYY format), the second textbox show the result, 31.

can any one help to get this,

Thanks in advance.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:35
Joined
Jul 9, 2003
Messages
16,268
I don't think there's a specific function that returns this answer. However if you think about it, the last day of the month is also the number of days in the month so that's your answer!

I know there are solutions to this problem (finding the last day of the month) so have a search and see what you can find.

It's difficult to write a function to find the last day of the month, however I recall the solution I saw because was very clever.

What you do is you find the first day of the next month and then take one off. That will give you the last day of the month.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 09:35
Joined
Sep 21, 2011
Messages
14,217
Google is your friend. ;)

Code:
MonthDays = Day(DateSerial(Year(Date), myMonth + 1, 1) - 1)

You supply the year and month numbers
The code above assumes current year with myMonth being the variable item.

HTH
 

Jamaluddin Jamal

Registered User.
Local time
Today, 12:35
Joined
Apr 15, 2017
Messages
13
I don't think there's a specific function that Returns this answer. However if you think about it, the last day of the month is also the number of days in the month so that's your answer!

I know there are solutions to this problem (finding the last day of the month) so have a search and see what you can find.

It's difficult to write a function to find the last day of the month, however I recall the solution I saw because was very clever.

What you do is you find the first day of the next month and then take one off.That will give you the last day of the month.

Thanks. I solved it with DateSerial function.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:35
Joined
Jul 9, 2003
Messages
16,268
Code:
MonthDays = Day(DateSerial(Year(Date), myMonth + 1, 1) - 1)

That looks like the solution I was thinking of, you supply it with "myMonth" then you add one month on "myMonth + 1". You want the 1st day of that month "myMonth + 1, 1) ". Then you take one day off and that's your answer... "myMonth + 1, 1) - 1) "
 
Last edited:

Galaxiom

Super Moderator
Staff member
Local time
Today, 18:35
Joined
Jan 20, 2009
Messages
12,851
Simpler still
Code:
MonthDays = Day(DateSerial(myYear, myMonth + 1, 0)
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:35
Joined
Feb 28, 2001
Messages
27,122
Egad, they ALLOW that solution, Greg? I'd almost say that feeding a 0th day of the month violates good programming concepts. That it works without error is amazing. And in its own little way, a bit weird.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 18:35
Joined
Jan 20, 2009
Messages
12,851
Egad, they ALLOW that solution, Greg? I'd almost say that feeding a 0th day of the month violates good programming concepts. That it works without error is amazing. And in its own little way, a bit weird.

Definitely weird but quite handy.

Any integers work as Month and Day parameters in DateSerial.

Imagine the complexity of having to validate the day parameter against both the month and year parameters before doing the calculation.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:35
Joined
Jul 9, 2003
Messages
16,268
I always have a problem with these "like" shortcode methods of coding.

A bit like the 1+ device I think in Javascript for doing an iteration like counter = counter + 1 ...

I think it's important for code to be readable in itself, so that it's obvious what it's doing.



Sent from my SM-G925F using Tapatalk
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 18:35
Joined
Jan 20, 2009
Messages
12,851
I think it's important for code to be readable in itself, so that it's obvious what it's doing.

It is all a matter of familiarity with language constructs. Personally I find a concise expression using the zeroth day of a month is perfectly clear.

A bit like the 1+ device I think in Javascript for doing an iteration like counter = counter + 1 ...

The increment and decrement operators are also used in C and PHP. They are perfectly valid language constructs. Criticisng them is like claiming Latin languages are compromised because they don't require the personal pronouns that are used in English.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:35
Joined
Feb 28, 2001
Messages
27,122
A bit like the 1+ device I think in Javascript for doing an iteration like counter = counter + 1 ...

Actually, let's take that one step farther back. That construct comes directly from the PDP Macro-11 Assembler because that is originally an addressing mode called "autoincrement" (and the corresponding "autodecrement" via prefixed minus-sign is there too). It originated on the PDP-11, which was the machine on which the Bell Labs scientists Brian Kernighan and Dennis Ritchie first implemented C as a series of assembler macros. They only used the autoincrement and autodecrement for stepping through arrays when processing the members of the arrays.

The really UGLY truth is that the only two machines in the world (at the time) that could efficiently execute C were the PDP-11 and later, the VAX 11/780. No other machine was quite so good at it. Last I checked, Intel machines don't do so well with it, either. Oddly enough, though early Motorola chips were more limited, once you got to the M6800 series and its many & varied descendants, that chip became able to perform C functions efficiently. Partly because the M6800 was the single-address machine that otherwise looked like the two-address PDP-11 systems.
 

Users who are viewing this thread

Top Bottom