算法从日期加上或减去天? [英] Algorithm to add or subtract days from a date?

查看:162
本文介绍了算法从日期加上或减去天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在尝试学习C ++写一个Date类。<​​/ P>

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

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

解决方案

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

您可以在这里找到的算法:<一href="http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html">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

这篇关于算法从日期加上或减去天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆