TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Pick Birthdays between 2 dates Irrespective of Year of Birth in C#
Deepti Velaga
Sep 02
2014
Code
1.9
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
Many times we come across situations where we have to get birthdays of employees between 2 given dates irrespective of year of birth.
Here is a small piece of code in c# that does the trick:
public
bool
IsBirthdayBetweenDates(DateTime birthdate, DateTime beginDate, DateTime endDate)
{
if
(birthdate !=
null
& beginDate !=
null
&& endDate !=
null
)
{
DateTime temp = birthdate.AddYears(beginDate.Year - birthdate.Year);
if
(temp < beginDate)
{
temp = temp.AddYears(1);
}
return
birthdate <= endDate && temp >= beginDate && temp <= endDate;
}
return
false
;
}
Here beginDate is the start date for search
endDate: end date for search
The logic is simple,Just bring the birthdate to the year of begindate by adding years and then compare the dates.
C#
C# Programming