Re: The month of February
I had modified your code....: :wave:
Code:
Option Explicit
Dim intMonth As Integer
Private Sub cmdMonth_Click()
intMonth = CInt(InputBox("Please enter a month 1 to 12"))
If intMonth < 1 Or intMonth > 12 Then
MsgBox "Please enter a number between 1 and 12"
Exit Sub '~~~> Exit the sub, ie. without executing the rest of the code
End If
Select Case intMonth
Case 1, 3, 5, 7, 8, 10, 12
txtDays.Text = "31"
Case 4, 6, 9, 11
txtDays.Text = "30"
Case 2
If bIsLeapYear(Year(Date)) = True Then '~~~> You have to pass the year for checking whether it is Leap Year or not. Here, I am passing the current year to the function.
txtDays.Text = "29"
Else
txtDays.Text = "28"
End If
End Select
End Sub
Function bIsLeapYear(ByVal inYear As Integer) As Boolean
bIsLeapYear = ((inYear Mod 4 = 0) _
And (inYear Mod 100 <> 0) _
Or (inYear Mod 400 = 0))
End Function
Re: The month of February
This is super easy with DateSerial(). It's a one-liner, in fact.
vb Code:
Option Explicit
Private Sub cmdMonth_Click()
Dim lngMonth As Long
lngMonth = Val(InputBox("Please enter a month 1 to 12"))
Select Case lngMonth
Case 0 ' User cancelled the InputBox
Case 1 To 12: txtDays.Text = Day(DateSerial(Year(Now()), lngMonth + 1, 0))
Case Else: MsgBox "Please enter a number between 1 and 12"
End Select
End Sub
To be clear, let's take a closer look at the one line of code that does all the calculating:
txtDays.Text = Day(DateSerial(Year(Now()), lngMonth + 1, 0))
DateSerial() takes three arguments: Year, Month, Day
- the Year argument is set to the current year: Year(Now())
- the Month argument is set to the month after the user-specified month by adding 1 to whatever the user specified. Note that if you send 13 as the month argument, DateSerial() will automatically roll over to January of the following year. So this one-liner technique works for all months, even December.
- the Day argument is set to 0. Much like how the month rolls over if you send 13, sending 0 for the Day rolls back to the last day of the previous month. Specifically, it returns "one day before (less than) the first."
So by sending month+1 to roll the month forward and day 0 to roll back one day we always get the last day of the specified month. The Day() function simply returns the DayOfMonth part of the date returned by DateSerial().
Re: The month of February
thank you very much
cheers
Re: The month of February
If your problem is solved, please Mark the Thread as Resolved... (you can find this option in Thread Tools, situated at the top of this page..) :wave:
Re: The month of February
vb Code:
Option Explicit
Private Function IsThisALeapYear(ByVal myYear As Integer) As Boolean
IsThisALeapYear = (29 = Day(DateSerial(myYear, 2, 29)))
End Function
Private Sub Command1_Click()
If IsThisALeapYear(2010) = True Then
MsgBox "Yep"
Else
MsgBox "Nope"
End If
End Sub
Re: The month of February
A truly simple way to get nr days in any month, any year. Try it.
Code:
NrDays = Day(DateSerial(theYear, theMonth+1, 0))
Edited: Ellis Dee posted this same cheat in #3 above. Oops on me.
Re: The month of February
But, if you are looking for whether a year is a leap year or not, why are you checking any month other than Februray?
Re: The month of February
Quote:
Originally Posted by
Hack
But, if you are looking for whether a year is a leap year or not, why are you checking any month other than Februray?
Per OP's post: "... that tells the user the number of days in a month after they enter the number of the month for the year they are in..."
The leap year issue, IMO, was due to not being able to determine the correct number of days for February. Using Ellis Dee's snippet, no need to know if leap year or not for the purpose of returning the number of days in a month.
Re: The month of February
Ah..then how about being able to throw in a month number and a year number and have it return how many days there was in that particular month for that particular year.
vb Code:
Option Explicit
Private Function DaysInMonth(ByVal iMonth As Integer, ByVal iYear As Integer) As Integer
Dim dtPlaceHolder As Date
dtPlaceHolder = DateSerial(iYear, iMonth, 1)
DaysInMonth = DateAdd("M", 1, dtPlaceHolder) - dtPlaceHolder
End Function
Private Sub Command1_Click()
Dim iDays As Integer
iDays = DaysInMonth(2, 2000)
MsgBox iDays
End Sub
Or am I just getting carried away?
Re: The month of February
Hack, that's what Ellis Dee's snippet does ;)
Day(DateSerial(iYear, iMonth + 1, 0)) replaces any custom function for that purpose.
Re: The month of February
One more version....
vb Code:
Private Sub Command1_Click()
MsgBox IsLeapYear(2004)
End Sub
Function IsLeapYear(SomeValue As Variant) As Boolean
IsLeapYear = IsDate("2/29/" & SomeValue)
End Function
Re: The month of February
sorry guys, I forgot to mention I need to incorporate the use all the following logical operators in my program: AND, OR and NOT which should be added to my initial code so that if the program is run in a leap year the program tells the user there are 29 days in February..
Please therefore try and incorporate the below code and include the NOT operator as well
Code:
Function bIsLeapYear(ByVal inYear As Integer) As Boolean
bIsLeapYear = ((inYear Mod 4 = 0) _
And (inYear Mod 100 <> 0) _
Or (inYear Mod 400 = 0))
End Function
Thanks in advance
Re: The month of February
Have you tried my code...????
Re: The month of February
No thanks. I have no interest in providing suboptimal code simply to conform to homework guidelines.
Re: The month of February
yes akhilesh I tried your code and it works but I need to use the code below so I make use of AND OR NOT operators in my program
Id be grateful for any assistance?
Re: The month of February
In North London, that is near to Greenwich, this teacher is an astronomer. He knews only this pseudocode:
Code:
if year modulo 400 is 0
then is_leap_year
else if year modulo 100 is 0
then not_leap_year
else if year modulo 4 is 0
then is_leap_year
else
not_leap_year
and then
Code:
if (year modulo 4 is 0) and (year modulo 100 is not 0) or (year modulo 400 is 0)
then is_leap_year
else
not_leap_year
Re: The month of February
ahn
Seems like an extra set of parentheses would be helpful -- is it
.. ( A and B ) or C, or
.. A and ( B or C ) ?
Spoo
Re: The month of February
Quote:
Originally Posted by
Spoo
ahn
Seems like an extra set of parentheses would be helpful -- is it
.. ( A and B ) or C, or
.. A and ( B or C ) ?
Spoo
Yes, for sure. But VB has a rule to deal with these operators if there is no parentheses. Try to find out yourself.
The pseudocode above is not VB code, its source is Wiki.
As Ellis, I don't want to go further because I don't want to be an astronomer.
Re: The month of February
ahn
Yes, thanks, I'm aware of the relative priority of and and or.
Hence my phrasing "would be helpful" as opposed to "is required".
I wonder if Wiki operators are treated as VB operators.
Spoo