-
Aug 31st, 2014, 03:44 AM
#1
Thread Starter
Lively Member
[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.
-
Aug 31st, 2014, 03:55 AM
#2
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.
-
Aug 31st, 2014, 04:24 AM
#3
Thread Starter
Lively Member
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.
-
Aug 31st, 2014, 05:14 AM
#4
Re: What's wrong in this code ? Please Help.
Originally Posted by kamalkumar2023
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
-
Aug 31st, 2014, 06:31 AM
#5
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
-
Aug 31st, 2014, 07:48 AM
#6
Re: What's wrong in this code ? Please Help.
Originally Posted by VBClassicRocks
Code:
DaysInMonth = Day(DateSerial(YearValue, MonthValue + 1, 0))
The classic 1-liner everyone wishes they knew about when they created their first calendar project
-
Aug 31st, 2014, 08:40 AM
#7
Thread Starter
Lively Member
Re: What's wrong in this code ? Please Help.
-
Aug 31st, 2014, 12:20 PM
#8
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.
-
Aug 31st, 2014, 02:51 PM
#9
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
-
Aug 31st, 2014, 03:27 PM
#10
Re: What's wrong in this code ? Please Help.
Originally Posted by Nightwalker83
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.
-
Aug 31st, 2014, 09:07 PM
#11
Re: What's wrong in this code ? Please Help.
Originally Posted by DataMiser
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
-
Aug 31st, 2014, 10:07 PM
#12
Re: What's wrong in this code ? Please Help.
Originally Posted by Nightwalker83
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
-
Sep 1st, 2014, 01:55 AM
#13
Re: What's wrong in this code ? Please Help.
Originally Posted by LaVolpe
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
-
Sep 1st, 2014, 12:16 PM
#14
Re: What's wrong in this code ? Please Help.
Originally Posted by Nightwalker83
... 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|