Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have the difference between two dates as 42 days, I want a program for apply penalty value on it, first as monthwise(it is 3), weekwise(it is 2), daywise(it is 1) respectively. What is C# code for this?
Posted
Updated 23-Jan-14 23:53pm
v2
Comments
Vladimir Svyatski 24-Jan-14 5:55am    
It heavily depends on a month. Each month has different number of days, you know.

It's not that simple: the "difference" between to dates is a timespan - which is not related to any particular day of the year, and so can't be expressed as a "months", "weeks" and "days" count - the numbers you would get would vary considerably if the start date was February 27th to those you would get for March 27th for example.
What you want is a timespan that is relative to a particular date - an Age: Working with Age: it's not the same as a TimeSpan![^]
 
Share this answer
 
I don't think it is possible or logical.you can't use datetime in the code or you can't say i need a report of February 31st.you have to define yourself.you can create a table to do this.
say you have a table with this fields monthName, dates etc.
 
Share this answer
 
I would start with an array of all the months with the number of days they have in them. This would allow you to work around what has been stated above.

The biggest questions I would start with to work around. Is one month 28, 29, 30, or 31 days or does one month mean exactly the same date next month < ambiguous I know but let me show you. In a normal month lets say Jan. there might be 31 days. A month from the 2nd of January would be the 2nd of February. The above array would work through that. The biggest problem you are going to run into is that today is the 29th. 1 month from today is what? the 29th of February so does it become the 28th of February or the 1st of March.

Using the above array and resolving the issue of a month from now not really existing, or saying a month from now is the number of days equal to the current month, therefore a month from the 31st of January would be the 3rd of March (31 days since Jan. has 31 days). Anyway there are several ways to solve that. After that is solved its pretty trivial algebra.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900