Word Hacks [Electronic resources]

Andrew Savikas

نسخه متنی -صفحه : 162/ 104
نمايش فراداده

Hack 71 Hack DATE Fields

Perform advanced date calculations with this grab bag of field hacks.

Dates are an important part of many Word documents, especially leases and contracts. When these dates involve calculations (such as "What date is 6 months from today?" or "How old is someone born on September 12, 1978?"), you can let Word do the grunt work for you.

When following the examples in this hack, press Ctrl-F9 or select InsertField for each set of braces in the field (most of the fields in this hack use several nested fields), taking care to put them in the correct ordersome are nested, and some are adjacentand then fill in the field codes as shown. In some cases, you'll need to format parts of the field code, which will be described in the text. Select the entire field and press F9 to display its results. Line breaks, indicated with the " character, are included to aid in readability, but they are not required for the fields to work properly.

8.3.1 Display Long Forms of Dates

To include the day of week with today's date, use the following field:

{DATE \@ "dddd 'the {DATE \@ d \*Ordinal} of' MMMM, yyyy"}

The field displays today's date in the form below:

Monday the 5th of July, 2004

Word fields ordinarily can't display date ordinals with superscripting. The following example shows you how to overcome this limitation. Select the characters shown in bold, choose FormatFont, and check the Superscript box:

{DATE \@ "dddd 'the' d'{IF{=(mod({DATE \@ d},10)<4)*
(mod({DATE \@ d},10)<>0)*({DATE \@ d}<>11)*
({DATE \@ d}<>12)*({DATE \@ d}<>13)}= 1 
{=mod({DATE \@ d},10)-2 \# rd;st;nd} th} of' MMMM, yyyy"}

The field displays the same result shown above, but with the ordinal superscripted:

Monday the 5th of July, 2004

8.3.2 Determine a Date's Place in the Year

The following field shows you what number day of the year today's date is. Again, you should superscript the characters shown in bold.

{QUOTE
"Today's date ({DATE \@ "MMMM d yyyy"}) falls on the "
{SET yd{={DATE \@ d}+INT(({DATE \@ M}-0.986)*30.575)-
IF({DATE \@ M}>2,2-(MOD({DATE \@ yy},4)=0)-
(MOD({DATE \@ yyyy},400)=0)+(MOD({DATE \@ yy},100)=0),0)}}
{=yd \# 0}
{IF{=(mod(yd,10)<4)*(mod(yd,10)<>0)*(mod(yd,100)<>11)*(mod(yd,100)<>12)*
(mod(yd,100)<>13)}= 1 {=mod(yd,10)-2 \# rd;st;nd} th}" day of the year."}

The field displays today's date in the form below:

Today's date (July 24 2004) falls on the 206th day of the year.

8.3.3 Automatically Insert a Past or Future Date

The following fields calculate any past or future date from today's date, based on the Gregorian calendar. Setting the Delay parameter in the fields allows for the required date to be many days, months, and/or years ahead or behind (to achieve the latter, express the Delay parameter as a negative number).

You can change the DATE parameters in the fields to CREATEDATE, SAVEDATE, or PRINTDATE to modify the date from which you calculate, if needed.

If you change the DATE parameter to SAVEDATE or PRINTDATE, you must save or print the document to get a valid output.

8.3.3.1 Calculate a month using n months delay

The following will display the name of the month 10 months from today's date:

{QUOTE{Set Delay 10}{=MOD({DATE \@ M}+Delay-1,12)+1}/00 \@ MMMM}

8.3.3.2 Calculate a month and year using n months delay

The following displays the month and year 10 months from today's date:

{QUOTE{SET Delay 10}
{SET m"{=MOD({DATE \@ MM}+Delay-1,12)+1}/0"}
{SET y{={DATE \@ yyyy}+INT((Delay+{DATE \@ M}-1)/12)}}
{m \@ MMMM}160{y}}

The above field uses an undocumented switch, "{FieldResult}/0", to store the result of the month calculation in a form that Word recognizes as a month. Note also the use of ASCII character 160 to create a nonbreaking space.

8.3.3.3 Calculate a month and year using n years delay

The following field displays the month and year one year from today:

{QUOTE{SET Delay 1}{DATE \@ MMMM}160{={DATE \@ yyyy}+Delay}}

8.3.3.4 Calculate a day, date, month, and year using n years delay

The following field displays the full date exactly one year from today, and is considerably more complicated than the previous example, because it accounts for leap years:

{QUOTE
{SET Delay 1}
{SET yy{={DATE \@ yyyy}+Delay}}
{SET dd{={DATE \@ d}-({DATE \@ d}>28)*({DATE \@ M}=2)*
((MOD(yy,4)>0)+(MOD(yy,400)>0)-(MOD(yy,100)>0))}}
{=dd*10^6+{DATE \@ M}*10^4+yy \# "00'-'00'-'0000"} \@ "dddd, MMMM d yyyy"}

8.3.3.5 Calculate a day, date, month, and year using n months delay

This field displays the full date exactly 10 months from today, taking into account leap years and differences in the length of months:

{QUOTE
{SET Delay 10}
{SET mm{=MOD({DATE \@ M}+Delay-1,12)+1}}
{SET yy{={DATE \@ yyyy}+INT((Delay+{DATE \@ M}-1)/12)}}
{SET dd{=IF(({DATE \@ d}>28)*(mm=2)*((MOD(yy,4)=0)+
(MOD(yy,400)=0)-(MOD(yy,100)=0))=1,28,IF((mm=4)+(mm=6)+(mm=9)+(mm=11)+
({DATE \@ d}>30)=1,30,{DATE \@ d}))}}
{=mm*10^6+dd*10^4+yy \# "00'-'00'-'0000"} \@ "dddd, MMMM d yyyy"}

8.3.3.6 Calculate a day, date, month, and year using n days delay

The following field displays the full date exactly 301 days from today. This field, and the next one, work by converting the date to a Julian day number, adding or subtracting the delay, then converting the new Julian day number back to a date.

{QUOTE
{SET Delay 301}
{SET a{=INT((14-{DATE \@ M})/12)}}
{SET b{={DATE \@ yyyy}+4800-a}}
{SET c{={DATE \@ M}+12*a-3}}
{SET d{DATE \@ d}}
{SET jd{=d+INT((153*c+2)/5)+365*b+INT(b/4)-
INT(b/100)+INT(b/400)-32045+Delay}}
{SET e{=INT((4*(jd+32044)+3)/146097)}}
{SET f{=jd+32044-INT(146097*e/4)}}
{SET g{=INT((4*f+3)/1461)}}
{SET h{=f-INT(1461*g/4)}}
{SET i{=INT((5*h+2)/153)}}
{SET dd{=h-INT((153*i+2)/5)+1}}
{SET mm{=i+3-12*INT(i/10)}}
{SET yy{=100*e+g-4800+INT(i/10)}}
{=mm*10^6+dd*10^4+yy \# "00'-'00'-'0000"} \@ "dddd, MMMM d yyyy"}

8.3.3.7 Calculate a day, date, month, and year using n weeks delay

The following field displays the full date exactly 43 weeks from today:

{QUOTE
{SET Delay 43}
{SET a{=INT((14-{DATE \@ M})/12)}}
{SET b{={DATE \@ yyyy}+4800-a}}
{SET c{={DATE \@ M}+12*a-3}}
{SET d{DATE \@ d}}
{SET jd{=d+INT((153*c+2)/5)+365*b+INT(b/4)-
INT(b/100)+INT(b/400)-32045+INT(Delay*7)}}
{SET e{=INT((4*(jd+32044)+3)/146097)}}
{SET f{=jd+32044-INT(146097*e/4)}}
{SET g{=INT((4*f+3)/1461)}}
{SET h{=f-INT(1461*g/4)}}
{SET i{=INT((5*h+2)/153)}}
{SET dd{=h-INT((153*i+2)/5)+1}}
{SET mm{=i+3-12*INT(i/10)}}
{SET yy{=100*e+g-4800+INT(i/10)}}
{=mm*10^6+dd*10^4+yy \# "00'-'00'-'0000"} \@ "dddd, MMMM d yyyy"}

8.3.3.8 Internationalizing dates

To use the above four examples with a "Day, Month, Year" format instead of the "Month Day, Year" format shown, you can change the date switches to suit your needs. For example, change each last line of the field codes in the previous four examples from:

{QUOTE{=mm*10^6+dd*10^4+yy \# "00'-'00'-'0000"} \@ "MMMM d yyyy"}."}

to this (changes are indicated in bold):

{QUOTE{=dd*10^6+mm*10^4+yy \# "00'-'00'-'0000"} \@ "d MMMM yyyy"}."}

8.3.4 Express a Date in Fiscal-Year Terms

The following field expresses today's date in fiscal-year terms, showing the year, month number, and week number within the month, assuming a July 1 to June 30 fiscal year:

{QUOTE
"Fiscal Year {date \@ yy}/{={DATE \@ yy}+1 \# 00}, Month: {=MOD(
{DATE \@ M}+5,12)+1}, Week: {=INT(({date \@ dd}-1)/7)+1}"}}

The field displays results like the following:

Fiscal Year 04/05, Month: 1, Week: 4

8.3.5 Calculate a Person's Age

The following field uses an ASK field for date of birth to perform an age calculation:

{QUOTE
{ASK BirthDate "What is the Birthdate?"}
{SET by {BirthDate \@ yyyy}}
{SET bm {BirthDate \@ M}}
{SET bd {BirthDate \@ d}}
{SET yy {DATE \@ yyyy}}
{SET mm {DATE \@ M}}
{SET dd {DATE \@ d}}
{SET md{=IF((mm=2),28+(mm=2)*((MOD(yy,4)=0)+(MOD
(yy,400)=0)-(MOD(yy,100)=0)),31-((mm=4)+(mm=6)+(mm=9)+(mm=11)))}}
{Set Years{=yy-by-(mm<bm)-(mm=bm)*(dd<bd)}}
{Set Months{=MOD(12+mm-bm-(dd<bd),12)}}
{Set Days{=MOD(md+dd-bd,md) \# 0}}
"If your Date of Birth was {
Birthdate \@ "MMMM d yyyy"}, then your age is {
Years} Year{IF{Years}= 1 " s}, {Months} Month{
IF{Months}= 1 " s} and {Days} Day{IF{Days}= 1 " s}."}

The field displays results like the following:

If your Date of Birth was September 12 1978, then your age is 25 Years, 10 Months and 
12 Days.

8.3.6 Use Print and Save Information in a Field

The following field reports whether or not a document has been saved since it was last printed:

{IF{PRINTDATE \@ yyyyMMddHHmm}>{SAVEDATE \@ yyyyMMddHHmm}
" Not Saved Since Last Print" "Not Printed Since Last Save"}

If you haven't saved your document since you last printed it, the field displays the following:

Not Saved Since Last Print

Paul Edstein