Creating a date adder
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
We are thinking about hiring more developers so I was asked to compose a test. I have been looking online for ideas. I found this question: https://stackoverflow.com/questions/23534528/how-to-add-n-days-to-a-date-in-java-without-importing-date-calendar-from-java-ap. I have attempted this myself as follows:
public class Date : IEquatable<Date>
private readonly static int _daysInMonth = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ;
private readonly int _month;
private readonly int _day;
private readonly int _year;
public Date(int day, int month, int year)
this._day = day;
this._month = month;
this._year = year;
//static because thinking about using in constructor for validation.
public static int MaxDays(int month, int year)
int maxDays = _daysInMonth[month - 1];
if (month == 2)
((year % 4 == 0) && (year % 100 != 0)))
maxDays = maxDays + 1;
return maxDays;
public Date AddDays(int daysToAdd)
int day = this._day + daysToAdd;
int month = this._month;
int year = this._year;
while (day > MaxDays(month,year))
day = day - MaxDays(month,year);
month++;
if (month > 12)
year++;
month = 1;
return new Date(day, month, year);
public override bool Equals(object obj)
return Equals(obj as Date);
public override int GetHashCode()
unchecked
int hash = 17;
hash = hash * 23 + _day.GetHashCode();
hash = hash * 23 + _month.GetHashCode();
hash = hash * 23 + _year.GetHashCode();
return hash;
public static bool operator ==(Date date1, Date date2)
public static bool operator !=(Date date1, Date date2)
return !(date1 == date2);
public bool Equals(Date other)
if (ReferenceEquals(other, null))
return false;
return _day == other._day && _month == other._month && _year == other._year;
I would be grateful for comments. I reviewed the terms of posting here: https://codereview.stackexchange.com/help/on-topic. This is real code (no stubs) that works and therefore I believe it is on topic.
c#
 |Â
show 3 more comments
up vote
0
down vote
favorite
We are thinking about hiring more developers so I was asked to compose a test. I have been looking online for ideas. I found this question: https://stackoverflow.com/questions/23534528/how-to-add-n-days-to-a-date-in-java-without-importing-date-calendar-from-java-ap. I have attempted this myself as follows:
public class Date : IEquatable<Date>
private readonly static int _daysInMonth = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ;
private readonly int _month;
private readonly int _day;
private readonly int _year;
public Date(int day, int month, int year)
this._day = day;
this._month = month;
this._year = year;
//static because thinking about using in constructor for validation.
public static int MaxDays(int month, int year)
int maxDays = _daysInMonth[month - 1];
if (month == 2)
((year % 4 == 0) && (year % 100 != 0)))
maxDays = maxDays + 1;
return maxDays;
public Date AddDays(int daysToAdd)
int day = this._day + daysToAdd;
int month = this._month;
int year = this._year;
while (day > MaxDays(month,year))
day = day - MaxDays(month,year);
month++;
if (month > 12)
year++;
month = 1;
return new Date(day, month, year);
public override bool Equals(object obj)
return Equals(obj as Date);
public override int GetHashCode()
unchecked
int hash = 17;
hash = hash * 23 + _day.GetHashCode();
hash = hash * 23 + _month.GetHashCode();
hash = hash * 23 + _year.GetHashCode();
return hash;
public static bool operator ==(Date date1, Date date2)
public static bool operator !=(Date date1, Date date2)
return !(date1 == date2);
public bool Equals(Date other)
if (ReferenceEquals(other, null))
return false;
return _day == other._day && _month == other._month && _year == other._year;
I would be grateful for comments. I reviewed the terms of posting here: https://codereview.stackexchange.com/help/on-topic. This is real code (no stubs) that works and therefore I believe it is on topic.
c#
2
If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
â Rick Davin
Feb 27 at 13:08
1
@RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on theDateTime
.
â t3chb0t
Feb 27 at 13:14
@Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
â w0051977
Feb 27 at 13:47
While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not useDateTime
or possiblyNodaTime
.
â Rick Davin
Feb 27 at 14:38
I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
â paparazzo
Feb 28 at 7:46
 |Â
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
We are thinking about hiring more developers so I was asked to compose a test. I have been looking online for ideas. I found this question: https://stackoverflow.com/questions/23534528/how-to-add-n-days-to-a-date-in-java-without-importing-date-calendar-from-java-ap. I have attempted this myself as follows:
public class Date : IEquatable<Date>
private readonly static int _daysInMonth = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ;
private readonly int _month;
private readonly int _day;
private readonly int _year;
public Date(int day, int month, int year)
this._day = day;
this._month = month;
this._year = year;
//static because thinking about using in constructor for validation.
public static int MaxDays(int month, int year)
int maxDays = _daysInMonth[month - 1];
if (month == 2)
((year % 4 == 0) && (year % 100 != 0)))
maxDays = maxDays + 1;
return maxDays;
public Date AddDays(int daysToAdd)
int day = this._day + daysToAdd;
int month = this._month;
int year = this._year;
while (day > MaxDays(month,year))
day = day - MaxDays(month,year);
month++;
if (month > 12)
year++;
month = 1;
return new Date(day, month, year);
public override bool Equals(object obj)
return Equals(obj as Date);
public override int GetHashCode()
unchecked
int hash = 17;
hash = hash * 23 + _day.GetHashCode();
hash = hash * 23 + _month.GetHashCode();
hash = hash * 23 + _year.GetHashCode();
return hash;
public static bool operator ==(Date date1, Date date2)
public static bool operator !=(Date date1, Date date2)
return !(date1 == date2);
public bool Equals(Date other)
if (ReferenceEquals(other, null))
return false;
return _day == other._day && _month == other._month && _year == other._year;
I would be grateful for comments. I reviewed the terms of posting here: https://codereview.stackexchange.com/help/on-topic. This is real code (no stubs) that works and therefore I believe it is on topic.
c#
We are thinking about hiring more developers so I was asked to compose a test. I have been looking online for ideas. I found this question: https://stackoverflow.com/questions/23534528/how-to-add-n-days-to-a-date-in-java-without-importing-date-calendar-from-java-ap. I have attempted this myself as follows:
public class Date : IEquatable<Date>
private readonly static int _daysInMonth = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ;
private readonly int _month;
private readonly int _day;
private readonly int _year;
public Date(int day, int month, int year)
this._day = day;
this._month = month;
this._year = year;
//static because thinking about using in constructor for validation.
public static int MaxDays(int month, int year)
int maxDays = _daysInMonth[month - 1];
if (month == 2)
((year % 4 == 0) && (year % 100 != 0)))
maxDays = maxDays + 1;
return maxDays;
public Date AddDays(int daysToAdd)
int day = this._day + daysToAdd;
int month = this._month;
int year = this._year;
while (day > MaxDays(month,year))
day = day - MaxDays(month,year);
month++;
if (month > 12)
year++;
month = 1;
return new Date(day, month, year);
public override bool Equals(object obj)
return Equals(obj as Date);
public override int GetHashCode()
unchecked
int hash = 17;
hash = hash * 23 + _day.GetHashCode();
hash = hash * 23 + _month.GetHashCode();
hash = hash * 23 + _year.GetHashCode();
return hash;
public static bool operator ==(Date date1, Date date2)
public static bool operator !=(Date date1, Date date2)
return !(date1 == date2);
public bool Equals(Date other)
if (ReferenceEquals(other, null))
return false;
return _day == other._day && _month == other._month && _year == other._year;
I would be grateful for comments. I reviewed the terms of posting here: https://codereview.stackexchange.com/help/on-topic. This is real code (no stubs) that works and therefore I believe it is on topic.
c#
edited Feb 27 at 8:13
asked Feb 27 at 8:02
w0051977
389112
389112
2
If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
â Rick Davin
Feb 27 at 13:08
1
@RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on theDateTime
.
â t3chb0t
Feb 27 at 13:14
@Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
â w0051977
Feb 27 at 13:47
While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not useDateTime
or possiblyNodaTime
.
â Rick Davin
Feb 27 at 14:38
I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
â paparazzo
Feb 28 at 7:46
 |Â
show 3 more comments
2
If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
â Rick Davin
Feb 27 at 13:08
1
@RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on theDateTime
.
â t3chb0t
Feb 27 at 13:14
@Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
â w0051977
Feb 27 at 13:47
While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not useDateTime
or possiblyNodaTime
.
â Rick Davin
Feb 27 at 14:38
I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
â paparazzo
Feb 28 at 7:46
2
2
If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
â Rick Davin
Feb 27 at 13:08
If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
â Rick Davin
Feb 27 at 13:08
1
1
@RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on the
DateTime
.â t3chb0t
Feb 27 at 13:14
@RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on the
DateTime
.â t3chb0t
Feb 27 at 13:14
@Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
â w0051977
Feb 27 at 13:47
@Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
â w0051977
Feb 27 at 13:47
While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not use
DateTime
or possibly NodaTime
.â Rick Davin
Feb 27 at 14:38
While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not use
DateTime
or possibly NodaTime
.â Rick Davin
Feb 27 at 14:38
I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
â paparazzo
Feb 28 at 7:46
I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
â paparazzo
Feb 28 at 7:46
 |Â
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188425%2fcreating-a-date-adder%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
â Rick Davin
Feb 27 at 13:08
1
@RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on the
DateTime
.â t3chb0t
Feb 27 at 13:14
@Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
â w0051977
Feb 27 at 13:47
While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not use
DateTime
or possiblyNodaTime
.â Rick Davin
Feb 27 at 14:38
I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
â paparazzo
Feb 28 at 7:46