473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Leap year problem

Here is the problem i am having: -

When a user logs in, on page load it will take the dob of all users in a stored friends list and compare them against the following conditions - To display any friends with a birthday inside the next 3 days, taking into account leap years for some1 born on the 29th of feb to be born on 28th in non leap years and also to display their age with the reminder. Here is code i have picked up so far.

Expand|Select|Wrap|Line Numbers
  1. 1.  vb.net
  2.     Public Function CalculateAge(ByVal birthday As DateTime) As Integer
  3.      Dim bd As DateTime
  4.      Try
  5.       ' Birthday this year
  6.       bd = New DateTime(DateTime.Today.Year, birthday.Month, birthday.Day)
  7.      Catch
  8.       ' no valid date, must be a leap year Then....
  9.       ' set birthday one day futher in time.
  10.       birthday = birthday.AddDays(1)
  11.       bd = New DateTime(DateTime.Today.Year, birthday.Month, birthday.Day)
  12.      End Try
  13.      Dim age As Integer = DateTime.Today.Year - birthday.Year
  14.      If (DateTime.Today < bd) Then age -= 1
  15.  
  16.      Return age
  17.     End Function
There is a problem with birthdays on Feb 29th in leap year, the statement: new DateTime(DateTime.Today.Year, birthday.Month, birthday.Day);
throws an exception.
If this year is NOT a leap year, you must first convert a birthday of 2/29/ to 2/28 (or 3/1 if you think that's when the person should be a year older!).

Expand|Select|Wrap|Line Numbers
  1.  2. 
  2. Private Sub LeapYearCheck()     
  3. Dim bLeapYear AsBoolean      
  4. bLeapYear = Date.IsLeapYear(Now.Year)     
  5. MessageBox.Show(bLeapYear)      
  6. bLeapYear = Date.IsLeapYear(2004)     
  7. MessageBox.Show(bLeapYear) End Sub
Expand|Select|Wrap|Line Numbers
  1. 3.
  2. Function IsLeapYear(ByVal SomeValue As Variant) As Boolean
  3.     On Error GoTo LocalError
  4.     Dim intYear As Integer
  5.     'The trick here is make sure that we get an integer
  6.     'The 3 Golden rules are:
  7.     'True if it is divisible by 4
  8.     'False if it is divisible by 100
  9.     'TRUE if it is divisble by 400
  10.     If IsDate(SomeValue) Then
  11.         intYear = CInt(Year(SomeValue))
  12.     Else
  13.         'try and get an integer from the parse
  14.         'does not matter if we get an error
  15.         'because the error trap will catch it
  16.         intYear = CInt(SomeValue)
  17.     End If
  18.     If TypeName(intYear) = "Integer" Then
  19.         IsLeapYear = ((intYear Mod 4 = 0) And _
  20.           (intYear Mod 100 <> 0) Or (intYear Mod 400 = 0))
  21.     End If
  22. Exit Function
  23. LocalError:
  24.     IsLeapYear = False
  25. End Function

I know i can use some fo this code to achieve this but i am unsure of the arrangement, any ideas?


Cheers
Dec 8 '08 #1
4 4387
kenobewan
4,871 Expert 4TB
@hallsers
I take this to mean that you have copied & pasted the code. It may better to spend the time learning how to do it and then you have a good idea next time.

There are many online resources, books or courses that can help. Try searching the site also as I'm sure this question has come up before.
Dec 8 '08 #2
No this is code that i have found myself on the internet and am unsure as to the arrangment of such code to my specific problem. I am unsure as to what code i can use in the problem and which variables can be used or modified to suit the situation. Any help would be appreciated. Thanku
Dec 8 '08 #3
any leap year examples on the site are in another language and not vb.net
I have attempted coding this into my Active Server Page with no luck so far.

any ideas would be welcome or a view on which way to approach this??
Dec 8 '08 #4
balabaster
797 Expert 512MB
This leads to the interesting question of "When does someone who is born on February 29th celebrate their birthday in non-leap years?"
Dec 9 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: deepak.rathore | last post by:
can someone give the regular expr. to validate leap yr like 02/29/2000,02/29/00 Thanks
2
by: owz | last post by:
Ok, I am making a program (java class file) 2 work out if a date entered is valid or invalid for the day, month year, and for leap years.. dd/mm/yyyy . I seem 2 have gotten it 2 validate the year...
8
by: rn216_ccc | last post by:
Hi there all, I found a web site,. http://www.onlineconversion.com/leapyear.htm, where it can give a list of leap years by within the given range by the user, or the user may enter a year and the...
5
by: Kasrav | last post by:
I have another problem hopeful am not bothering you guys too much if u can help that would be wonderful. I have this code here def year2(): print'This program validates days and months of the...
8
by: SONIQ | last post by:
You can determine whether a year is a leap year by testing if it is divisible by 4. However, years that are also divisible by 100 are not leap years, unless they are also divisible by 400, in which...
12
by: Brigitte Behrmann | last post by:
Can anyone assist with my code. I just cannot get this to work: I have to write a script that allows the user to enter a year & then determine whether it is a leap year or not. The request is for a...
37
by: mazwolfe | last post by:
I'm new here, so excuse me if my style is incorrect. Can anyone come up with a better method for this calculation? Code: int is_leap(int year) { switch (year % 19) { case 0: case 3: case 6:...
5
by: jimix | last post by:
here's what i have. using microsoft studio #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main( void ) { int year, b, c, e; b = 4; c = 100;
4
by: jrw133 | last post by:
Hello. So im a little bit stuck on one of my homework questions. heres the question: Write a C Shell Script that performs the following functions. Asks the user to input a year. The script...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.