Need a formula: Extracting Years from Seconds Since January 1, 0001 12:00 AM

If you need accuracy to the very second, you'll want a commercial-grade datetime package; it's just too complex to do accurately with a simple algorithm. For instance:

  • Many people have noted that we have leap years every 4 years, but did you know that every year divisible by 100 but not by 400 is not a leap-year? This has caused issues even in large commercial products.
  • Some countries do not observe daylight-savings, and the ones who do observe it do so at different times of the year. This changes arbitrarily from year-to-year.
  • Even in countries with DST, some states/cities arbitrarily choose not to use it.
  • Years before 1582 use a slightly different calendar
  • There were only 355 days in the year 1582 (or 354 in the year 1752, depending on the country).
  • There are major issues when countries switch timezones. Some countries even lose entire days!
  • Then there's leap-seconds. Some governing body decides arbitrarily whether or not we should add one (or sometimes, two) seconds to the clock each year. There's no way to know ahead of time when we'll have the next leap-second, and no pattern to past leap-seconds.

Because of these and more complications, you are better off not writing the code yourself, unless you can relax the constraint that you need accuracy to the very second over 12-million years.

"October 4, 1582 – Saint Teresa of Ávila dies. She is buried the next day, October 15."


Wikipeda has an article on Julian Date with an algorithm which you could adapt to your needs.