Results 1 to 20 of 20

Thread: The month of February

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2009
    Location
    North London
    Posts
    121

    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

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: The month of February

    This is super easy with DateSerial(). It's a one-liner, in fact.
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdMonth_Click()
    4.     Dim lngMonth As Long
    5.  
    6.     lngMonth = Val(InputBox("Please enter a month 1 to 12"))
    7.     Select Case lngMonth
    8.         Case 0 ' User cancelled the InputBox
    9.         Case 1 To 12: txtDays.Text = Day(DateSerial(Year(Now()), lngMonth + 1, 0))
    10.         Case Else: MsgBox "Please enter a number between 1 and 12"
    11.     End Select
    12. 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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2009
    Location
    North London
    Posts
    121

    Re: The month of February

    thank you very much

    cheers

  5. #5
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: The month of February

    vb Code:
    1. Option Explicit
    2.  
    3. Private Function IsThisALeapYear(ByVal myYear As Integer) As Boolean
    4.      IsThisALeapYear = (29 = Day(DateSerial(myYear, 2, 29)))
    5. End Function
    6.  
    7. Private Sub Command1_Click()
    8. If IsThisALeapYear(2010) = True Then
    9.    MsgBox "Yep"
    10. Else
    11.    MsgBox "Nope"
    12. End If
    13. End Sub

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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?

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: The month of February

    Quote Originally Posted by Hack View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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:
    1. Option Explicit
    2.  
    3. Private Function DaysInMonth(ByVal iMonth As Integer, ByVal iYear As Integer) As Integer
    4.     Dim dtPlaceHolder As Date
    5.     dtPlaceHolder = DateSerial(iYear, iMonth, 1)
    6.     DaysInMonth = DateAdd("M", 1, dtPlaceHolder) - dtPlaceHolder
    7. End Function
    8.  
    9. Private Sub Command1_Click()
    10. Dim iDays As Integer
    11. iDays = DaysInMonth(2, 2000)
    12. MsgBox iDays
    13. End Sub
    Or am I just getting carried away?

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: The month of February

    One more version....

    vb Code:
    1. Private Sub Command1_Click()
    2.     MsgBox IsLeapYear(2004)
    3. End Sub
    4.  
    5. Function IsLeapYear(SomeValue As Variant) As Boolean
    6.     IsLeapYear = IsDate("2/29/" & SomeValue)
    7. 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

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Nov 2009
    Location
    North London
    Posts
    121

    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

  14. #14
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  15. #15
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: The month of February

    No thanks. I have no interest in providing suboptimal code simply to conform to homework guidelines.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Nov 2009
    Location
    North London
    Posts
    121

    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?

  17. #17
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  18. #18
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  19. #19
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: The month of February

    Quote Originally Posted by Spoo View Post
    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.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  20. #20
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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
  •  



Click Here to Expand Forum to Full Width