|
-
Jan 24th, 2010, 10:29 PM
#1
Thread Starter
Lively Member
The month of February
Hey,
I created a simple program 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, however I need to now allow for leap years...I read the vb help files and discovered this code but I do not know how to implent it to my source code:-
Function bIsLeapYear(ByVal inYear As Integer) As Boolean
bIsLeapYear = ((inYear Mod 4 = 0) _
And (inYear Mod 100 <> 0) _
Or (inYear Mod 400 = 0))
Please can someone tell me how to use it in my below source code:-
Code:
Option Explicit
Dim Month As Integer
Private Sub cmdMonth_Click()
Month = InputBox("Please enter a month 1 to 12")
If Month < "1" Then
MsgBox "Please enter a number between 1 and 12"
Else
If Month > "12" Then
MsgBox "Please enter a number between 1 and 12"
End If
End If
Select Case Month
Case 1, 3, 5, 7, 8, 10, 12
txtDays.Text = 31
Case 4, 6, 9, 11
txtDays.Text = 30
Case 2
txtDays.Text = "28"
End Select
End Sub
thanks in advance...
cheers
-
Jan 24th, 2010, 11:39 PM
#2
Re: The month of February
I had modified your code....: 
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
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Jan 25th, 2010, 12:23 AM
#3
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().
Last edited by Ellis Dee; Jan 25th, 2010 at 12:28 AM.
-
Jan 25th, 2010, 04:49 AM
#4
Thread Starter
Lively Member
Re: The month of February
thank you very much
cheers
-
Jan 25th, 2010, 04:53 AM
#5
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..)
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Jan 25th, 2010, 06:55 AM
#6
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
-
Jan 25th, 2010, 08:29 AM
#7
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.
Last edited by LaVolpe; Jan 25th, 2010 at 09:21 AM.
-
Jan 25th, 2010, 11:31 AM
#8
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?
-
Jan 25th, 2010, 11:35 AM
#9
Re: The month of February
 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.
-
Jan 25th, 2010, 11:48 AM
#10
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?
-
Jan 25th, 2010, 12:03 PM
#11
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.
-
Jan 25th, 2010, 12:33 PM
#12
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 27th, 2010, 08:46 PM
#13
Thread Starter
Lively Member
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
-
Jan 27th, 2010, 08:52 PM
#14
Re: The month of February
Have you tried my code...????
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Jan 27th, 2010, 08:55 PM
#15
Re: The month of February
No thanks. I have no interest in providing suboptimal code simply to conform to homework guidelines.
-
Jan 27th, 2010, 09:22 PM
#16
Thread Starter
Lively Member
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?
-
Jan 27th, 2010, 10:13 PM
#17
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
-
Jan 27th, 2010, 10:32 PM
#18
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
-
Jan 27th, 2010, 10:43 PM
#19
Re: The month of February
 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.
-
Jan 27th, 2010, 11:01 PM
#20
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|