Results 1 to 14 of 14

Thread: [RESOLVED] What's wrong in this code ? Please Help.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2014
    Posts
    66

    Resolved [RESOLVED] What's wrong in this code ? Please Help.

    Hello All,

    Code:
    Private Sub Command1_Click()
    Dim totaldays As Integer
    
    totaldays = DateAdd("m", 1, Now) - Now
    MsgBox "Total Days in this Month = " & totaldays & ""
    
    End Sub
    THIS CODE SAYS THE TOTAL NUMBER OF DAYS IN THIS MONTH(AUGUST) = '30'

    WHAT'S WRONG IN THIS CODE ??
    PLEASE HELP..

    THANK YOU.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: What's wrong in this code ? Please Help.

    You're adding one month to Today, which is August 31st, and the adding 1 month to August 31st will be Sept 30th. Sept 31st is an illegal date and dateAdd will not return an illegal date, so there are thirty days to then end of the next month.
    If you were adding 1 month to Jan 31st, then the total days returned between Jan 31st and the end of the next month will be 28, or 29 on leap year.
    That bit of code will only be the length of the current month if the last day of then next month is greater than the current day of the current month.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2014
    Posts
    66

    Re: What's wrong in this code ? Please Help.

    OK Mr.Passel
    I understand it.
    Thanks

    then how to get the number of days in the current month ??

    Thanks again.

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: What's wrong in this code ? Please Help.

    Quote Originally Posted by kamalkumar2023 View Post
    then how to get the number of days in the current month ??
    Using the Now method

    Code:
    Dim totaldays As Integer
    totaldays = DateAdd("m", 1, Now) - Now
    Select Case Month(Now)
    Case 1 Or 3 Or 5 Or 7 Or 10 Or 12
     totaldays = 31
    Case 2
     If Now Mod 4 = 0 Then 
        totaldays = 29
     Else
        totaldays = 28
    End if
    Case 4 Or 6 Or 8 Or 11
     totaldays = 30
    End Select
    MsgBox "Total Days in this Month = " & totaldays & ""
    Although, this is probably not the best way to accomplish this since the user can just change the system time.
    Last edited by Nightwalker83; Aug 31st, 2014 at 05:24 AM. Reason: Fixed typo!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: What's wrong in this code ? Please Help.

    Code:
    Option Explicit
    Function DaysInMonth(YearValue As Long, MonthValue As Long) As Long
     DaysInMonth = Day(DateSerial(YearValue, MonthValue + 1, 0))
    End Function
    
    Private Sub Form_Load()
     Debug.Print DaysInMonth(Year(Now), Month(Now))
    End Sub

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

    Re: What's wrong in this code ? Please Help.

    Quote Originally Posted by VBClassicRocks View Post
    Code:
     DaysInMonth = Day(DateSerial(YearValue, MonthValue + 1, 0))
    The classic 1-liner everyone wishes they knew about when they created their first calendar project
    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}

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2014
    Posts
    66

    Re: What's wrong in this code ? Please Help.

    Thanks guys..

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] What's wrong in this code ? Please Help.

    Does that 1-liner account for leap year?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] What's wrong in this code ? Please Help.

    Why ask? Why not test? Seems it would be quicker?
    Yes, DateSerial will account for leap year.

    Test the current month
    Debug.Print Day(DateSerial(Year(Now), Month(Now) + 1, 0))

    Test a couple of leap year candidates
    Debug.Print Day(DateSerial(2000, 2 + 1, 0)) 'Prints 29 a leap year
    Debug.Print Day(DateSerial(1900, 2 + 1, 0)) 'Prints 28 not a leap year

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: What's wrong in this code ? Please Help.

    Quote Originally Posted by Nightwalker83 View Post
    Using the Now method

    Code:
    Dim totaldays As Integer
    totaldays = DateAdd("m", 1, Now) - Now
    Select Case Month(Now)
    Case 1 Or 3 Or 5 Or 7 Or 10 Or 12
     totaldays = 31
    Case 2
     If Now Mod 4 = 0 Then 
        totaldays = 29
     Else
        totaldays = 28
    End if
    Case 4 Or 6 Or 8 Or 11
     totaldays = 30
    End Select
    MsgBox "Total Days in this Month = " & totaldays & ""
    Although, this is probably not the best way to accomplish this since the user can just change the system time.
    It would return the wrong result for August as today is August 31st but the code would show only 30 days in August

    You also do not need the Or statements on the Cases
    Code:
    Case 1, 3, 5, 7, 8, 10, 12
    Last edited by DataMiser; Aug 31st, 2014 at 03:30 PM.

  11. #11
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: What's wrong in this code ? Please Help.

    Quote Originally Posted by DataMiser View Post
    It would return the wrong result for August as today is August 31st but the code would show only 30 days in August

    You also do not need the Or statements on the Cases
    Code:
    Case 1, 3, 5, 7, 8, 10, 12
    Oops, yes, August is suppose to be in the 31 days list not the 30 days list. I originally put a "," to separate the months but the IDE kicked up a fuss although, I just did it again and it worked. Anyway here is the new code with changes and also, the addition of September which, I missed last time.

    Code:
    Private Sub Command1_Click()
    Dim totaldays As Integer
    totaldays = DateAdd("m", 1, Now) - Now
    Select Case Month(Now)
    Case 1, 3, 5, 7, 8, 10, 12
     totaldays = 31
    Case 2
     If Now Mod 4 = 0 Then
        totaldays = 29
     Else
        totaldays = 28
    End If
    Case 4, 6, 9, 11
     totaldays = 30
    End Select
    MsgBox "Total Days in this Month = " & totaldays & ""
    End Sub
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

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

    Re: What's wrong in this code ? Please Help.

    Quote Originally Posted by Nightwalker83 View Post
    Code:
    Private Sub Command1_Click()
    Dim totaldays As Integer
    totaldays = DateAdd("m", 1, Now) - Now
    Select Case Month(Now)
    Case 1, 3, 5, 7, 8, 10, 12
     totaldays = 31
    Case 2
     If Now Mod 4 = 0 Then
        totaldays = 29
     Else
        totaldays = 28
    End If
    Case 4, 6, 9, 11
     totaldays = 30
    End Select
    MsgBox "Total Days in this Month = " & totaldays & ""
    End Sub
    The 1-liner works fine. But yours? Still a bug here or there....

    1) Now Mod 4 isn't correct. That can actually change depending on time of day, the same day. Think you wanted Year(Now) Mod 4

    2) Even if above is true, can't just assume divisible by 4 = leap year. As posted earlier, the year 1900 isn't a leap year, but 1900 Mod 4 is zero. In the year 2100 (I won't be around), it isn't a leap year either.

    Interesting tidbit about leap years on the century mark: not a leap year unless Mod 400 is zero
    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}

  13. #13
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: What's wrong in this code ? Please Help.

    Quote Originally Posted by LaVolpe View Post
    2) Even if above is true, can't just assume divisible by 4 = leap year. As posted earlier, the year 1900 isn't a leap year, but 1900 Mod 4 is zero. In the year 2100 (I won't be around), it isn't a leap year either.
    What! Why did they decide to change it? It seems strange!

    Edit:

    Just searched on google and the rule for a leap year is 365 + 1/4 − 1/100 + 1/400 = 365.2425. I was unaware of the part in bold! I still do not understand why they have that extra part?
    Last edited by Nightwalker83; Sep 1st, 2014 at 01:59 AM.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  14. #14
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: What's wrong in this code ? Please Help.

    Quote Originally Posted by Nightwalker83 View Post
    ... the rule for a leap year is 365 + 1/4 − 1/100 + 1/400 = 365.2425. I was unaware of the part in bold! I still do not understand why they have that extra part?
    Because the earth doesn't rotate around the Sun exactly 365.25 rotations of the earth.
    It is close to 365.25, but every 100 years, of adding a leap year every 4th year, you now have almost an extra accumlated (about 3/4 of a day), so if you skip adding a leap day that year, instead of being 3/4 of a day over, you'll be 1/4 of a day short.
    Do that for a period of 400 years, and now you're short about a day, so every 400 years, don't skip the leap day that year.
    By using those 2nd and 3rd order terms, it allows the current calendar to be correct within a day for I believe over 3000 years, but I would have to look that up to verify if that is the number.

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