算法从日期加上或减去天?算法、日期

2023-09-10 23:20:35 作者:残霜

我试图在尝试学习C ++写一个Date类。

我试图找到一个算法,加上或减去天的日期,其中日开始,从1月和从1开始它被证明是非常复杂的,和谷歌不转了很多,

有谁知道一种算法,做到这一点的?

解决方案

最简单的方法是实际写两个功能,一是这一天从给定的起始日期转换为天数,然后又它转换回日期。一旦日期是pssed作为天数前$ P $,是微不足道的加或减,以它

您可以在这里找到的算法:http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html

I'm trying to write a Date class in an attempt to learn C++.

算法时间 个人笔记

I'm trying to find an algorithm to add or subtract days to a date, where Day starts from 1 and Month starts from 1. It's proving to be very complex, and google doesn't turn up much,

Does anyone know of an algorithm which does this?

解决方案

The easiest way is to actually write two functions, one which converts the day to a number of days from a given start date, then another which converts back to a date. Once the date is expressed as a number of days, it's trivial to add or subtract to it.

You can find the algorithms here: http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html