subtracting one month from current month to display month name w/

D

Diane

From this forum, I was able to find a script that will subtract one month
from the current date for a "date field" in my document.
Script:
{QUOTE}
{SET Delay 4}
{SET m{ =mod({ DATE \@ MM } -Delay +11,12) +1}}
{SET y{=INT({ DATE \@yyyy} + (Delay + { DATE \@ M } -1) /12 }}
"{ m }-{ y}" \@ "MMMM yyyy"}

The script works, but I do not understand what exactly line 3, the "SET M"
is doing at the part of "11,12) +1. I understand it takes current month and
subtracts 4 months in my example, but then what is the +11,12) +1 doing?

My second problem is, as in this example, I am subtracting four months,
which should give me, December 2006 - instead, I get December 2007. The year
does not calculate based on the month. Although, when I test adding 23
months, versus subtracting 4 months, then my year adjusts correctly; it's
somewhere in the subtracting that I'm having an issue with. All I need/want
is to subtract one month from the current month, but if that month is
January, the year needs to adjust as well.

Any clarification on this script would be appreciated!

Diane
 
P

Peter Jamieson

What the 11,12) is supposed to be doing is to do with the mod function -
mod(x,y) in essence does an "integer" divide of x by y and returns the
remainder, e.g.

mod(1,2) is 1
mod(2,2) is 0
mod(3,2) is 1
mod(4,2) is 0
mod(5,2) is 1
and so on.

So

mod (1,12) is 1
mod(12,12) is 0
mod(13,12) is 1
mod(14,12) is 2
and so on.

So the formula is trying to calculate the correct result month given that a
year only has 12 months.

However, this formula doesn't work, as you have discovered. I'd follow the
link Tony Jollans has given you, but if you want a completely different
approach that should also work, try

http://tips.pjmsn.me.uk/t0004.htm

Peter Jamieson
 
D

Diane

Peter & Tony,
Thanks for the helpful links and also the explanation of the script. The
example that I had posted comes from the MSWORD DOC - DateCalc V2_6 which is
the link Peter pointed me to in his last post, so prior to my posting I
already found this script - although had some trouble understanding it. From
this document, I am using the example given for "Calculate a month and year,
using n months delay". With this example, should I expect the correct
calculation for years when subtracting 13 months or in my example 4 months?
If it should work for this scenario, then I'm subtracting wrong somewhere in
this script.
Again, thanks for the links posted here, all include great detail of
information.
Diane
 
P

Peter Jamieson

Hi Diane,

You have

{QUOTE}
{SET Delay 4}
{SET m{ =mod({ DATE \@ MM } -Delay +11,12) +1}}
{SET y{=INT({ DATE \@yyyy} + (Delay + { DATE \@ M } -1) /12 }}
"{ m }-{ y}" \@ "MMMM yyyy"}

when you probably need

{QUOTE
{SET Delay -4}
{SET m{ =mod({ DATE \@ MM } +Delay +11,12) +1}}
{SET y{=INT({ DATE \@yyyy} + (Delay + { DATE \@ M } -1) /12 }}
"{ m }-{ y}" \@ "MMMM yyyy"}

(i.e. use a negative value for Delay, fix the sign in the third line, and
that } after "QUOTE" is probably a typo in your post here.)

Peter Jamieson
 
D

Diane

Peter,
That was it - I was subtracting at the wrong position! I did not realize I
should have been inputting a negative number for the delay.
Many Thanks!!
Diane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top