Accounting for leap year in comparing year to year

2019-07-13 17:01发布

I am writing a program that shows the current years sales from the beginning of the fiscal year to the current date, compared to the same date range of the year before.

My question is, what efforts do I need to take for leap year?

UPDATE:

OK they want it like I said (compare last year up to same date) but if today is non leap year and last year is and today is feb 28th compare to last year up to 29th. Or if today is Feb 29th compare to last year up to 28th.

4条回答
家丑人穷心不美
2楼-- · 2019-07-13 17:27

This strikes me as a business decision. Depending on the type of business, that extra day may not matter. Otherwise, I suppose you could treat is as "first n days of the year" rather than "Jan 1 through X".

查看更多
放荡不羁爱自由
3楼-- · 2019-07-13 17:29

Surely that depends on what the business wants you to do. Isn't this a question that should be answered by an accountant?

查看更多
beautiful°
4楼-- · 2019-07-13 17:31

Here's an idea, but like others have said it might be based on your specific domain.

  1. Consider 1 "normalized" year = 365.242199 days counting all the leap stuff (says google)
  2. Calculate the average sales per day in your year based on the real number of days in that year
  3. Scale it up or down to sales per 365.242199 days

So for example

   2007 = $4000 in sales.
   There's 365 days in 2007, so avg sale per day = $10.96
   Multpiplying times num days in a normalized year (365.242199) 
      gives you $4003.05 normalized sales

You can compare this directly to a similar calculation for 2008,

   2008 = $5000 in sales.
   There's 366 days in 2008, so avg sale per day = $13.66
   Multpiplying times num days in a normalized year (365.242199) 
      gives you $4975.655 normalized sales for 2008!
查看更多
叼着烟拽天下
5楼-- · 2019-07-13 17:43

You could scale down the leap year values to take the extra day into account.

So if you compare, say, the 1st of September of a regular year with the 1st of September of a leap year you would do:

if(year == leapyear && day > 28Feb)
    Convert date to dayOfYear
    leapYearValue *= dayOfYear / (dayOfYear + 1)

This should really be in the specification, though.

查看更多
登录 后发表回答