Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] add/subtract with date!

From: t_degrav::rhrk::uni-kl::de at: 14-Nov-2000 1:23

Ok, I'm not sure if this message came through the first time... :-( _____________ Hi there! I'm wondering how to calculate when using date! values... As long as I only want to add/subtract a certain amount of DAYS there seem to be no problems, but when it comes to MONTHS or YEARS it gets a little comlicated. Or am I missing something simple here??? Let's say I have:
>> date: 14-Nov-2000
== 14-Nov-2000 Now it's easy to say:
>> newdate: date + 1
== 15-Nov-2000
>> newdate: date + 100
== 22-Feb-2001
>> newdate: date - 1
== 13-Nov-2000
>> newdate: date - 400
== 11-Oct-1999 So, even roll-overs to another month or year are recognized. That's great! But how can I add/subtract exactly one month or one year? Using a certain amount of days would depend on which month I start with.
>> newdate: date + ???
== 14-Dec-2000
>> newdate: date + ???
== 14-Nov-2001
>> newdate: date - ???
== 14-Oct-2000
>> newdate: date - ???
== 14-Nov-1999 As far as I can see right now there's no way to do that directly, so I started experimenting with to-date. Now I can do (parenthesis only included for better readability):
>> newdate: to-date reduce [date/day (date/month + 1) date/year]
== 14-Dec-2000
>> newdate: to-date reduce [date/day (date/month + 10) date/year]
== 14-Sep-2001
>> newdate: to-date reduce [date/day date/month (date/year + 1)]
== 14-Nov-2001 An overflow of the month is correctly recognized and the year adjusted. But, when I try:
>> newdate: to-date reduce [date/day (date/month - 1) date/year]
== 14-Oct-2000
>> newdate: to-date reduce [date/day (date/month - 15) date/year]
== 14-Dec-23844
>> newdate: to-date reduce [date/day date/month (date/year - 1)]
== 14-Nov-1999 Wow, if I go back 15 months I jump ahead 21844 years! ;-) Ok, when I think about how 'to-date might work (or better how 'to does) I have to admit this result isn't very surprising... Final question: Is there any easy way of adding/subtracting months or years without having to check for boundaries myself??? TIA, Tom