DateCalc V2 - 85
DateCalc V2 - 85
INTRODUCTORY NOTES..................................................................................................1
Using this document..........................................................................................................1
Viewing and updating fields..............................................................................................1
Date input/output formats.................................................................................................1
Introductory Notes
This document shows how to do a wide range of date and time calculations using Microsoft
Word, versions 97 and later, using field coding instead of macros. Whilst macros are
sometimes simpler to use, using fields avoids some of the security issues associated with
macros.
Using this document
In this document, the bookmarks used to create an example are often re-used in other
examples. A side effect of this is that the calculated dates and times in the examples will
display a result based on the last-bookmarked entry for each reference when this document
is first opened. This can be corrected by updating the affected fields (by selecting the field
and pressing F9, for example), and would not affect documents where the bookmark names
are not re-used.
You can change the behaviour of many of the DATE fields in this document by expanding the
field codes (see “Viewing and updating fields”) and changing the ‘DATE’ expressions to
‘CREATEDATE’, ‘SAVEDATE’ or ‘PRINTDATE’, according to what you are trying to achieve.
See “Date Field Basics” for more details on the effect of such changes.
Viewing and updating fields
To view the fields in this document, I recommended that you toggle field shading on. You can
do this via Tools|Options|View (Word 97-2003) or Word Options|Advanced>Show Document
Content (Word 2007) and, under ‘Show’, selecting the ‘Always’ or ‘When selected’ options.
You’ll probably find the ‘Always’ option works best. Doing this has no effect on the printed
output but makes the fields easier to find and work with on-screen.
To update a given field, simply select it and press F9. Most will update without any further
action on your part, but the interactive examples will prompt you for input(s).
To see the inner workings of any field, select the whole field and press Shift-F9. When
you’re done, pressing Shift-F9 again will toggle the display back to showing the field’s
results. Alternatively, if you press F9, the field will update and toggle the display back to
showing the field’s results in one go.
Many of the date and time field examples in this document have been laid out with line feeds
to better show their structure when the field code is toggled ‘on’. The line feeds are
inconsequential and can be deleted. Some fields also include hard spaces (ASCII value 160)
to keep the results on the same line. These can be replaced with soft spaces (ASCII value
32) if you prefer.
Date input/output formats
Many date fields in this document are region-dependent and are coded to work on systems
using day-month-year regional settings. For system using month-day-year regional settings,
obtaining a correctly result is as simple as changing the order of the embedded fields coded
as ‘{dd}-{mm}-{yy}’to ‘{mm}-{dd}-{yy}’. Included in this document is a macro to automate the
change. Changing the order shouldn't be necessary for systems using day-month-year
settings; for those using month-day-year settings, it's a once-only change. To run the macro,
Press Alt-F8 and select the macro named ‘SwapDayMonthCodes’, then click ‘Run’.
Most date fields in this document display their output in an extended European format (i.e.
‘dddd, d MMMM yyyy’). You can change the output format by changing the field’s date
picture switch. This is independent of your system’s regional settings. To change the output
format for the affected fields, place the cursor AFTER these instructions, then press Alt+F9 to
display all the field codes. Next, press Ctrl+H to bring up the Find/Replace box and enter:
‘dddd, d MMMM yyyy’ in the Find box; and your preferred format (eg ‘MMMM d, yyyy’) in the
Replace box, then click ‘Replace All’. When it reaches the end of the document, Word will
1
WORD Date & Time Manipulation: Tips & Techniques
ask if you want to continue from the beginning. Choose ‘No’; otherwise you’ll change these
instructions too.
Input formats tend to be flexible, permitting ‘d MMMM yyyy’, ‘d MM yyyy’, ‘M, d yyyy’, and so
on.
2
WORD Date & Time Manipulation: Tips & Techniques
3
WORD Date & Time Manipulation: Tips & Techniques
4
WORD Date & Time Manipulation: Tips & Techniques
Descriptive Text
This can be any text that is enclosed in a pair of single quotation marks. For example,
you could add the string 'Greenwich Mean Time' or 'Australian Eastern Summer Time'
to the field. Note that the single quotation marks don’t display in the output.
So, how would you display an apostrophe, as in "Aug-14 ‘04"? Word’s help file
suggests you can use a single quote character for this (eg {DATE \@ "MMM-d, 'yy"})
but this doesn’t work, since the quote characters must be used in pairs. What does
work, however, is: {DATE \@ "MMM-d, {QUOTE 180}yy"}, as in Dec-12, ´09.
The DATE field
The DATE field inserts the current date into your document, and updates every time
you open the document or perform an action that causes the document fields to
update. This field is coded as {DATE} and the default format is 13/03/2025.
The CREATEDATE field
The CREATEDATE field inserts the date your document was created, and remains
unchanged unless you save the file using File|Save As. This field is coded as
{CREATEDATE} and displays as 24/03/2007 7:41 AM.
The SAVEDATE field
The SAVEDATE field inserts the date your document was last saved, and updates
whenever you save the document. This field is coded as {SAVEDATE} and displays
as
12/12/2009 12:59:00 PM.
The PRINTDATE field
The PRINTDATE field inserts the date your document was last printed, and updates
whenever you print the document. This field is coded as {PRINTDATE} and displays
as (this document has never been printed, hence the 0s).
The TIME field
The TIME field inserts the time date into your document, and updates every time you
open the document or perform an action that causes the document fields to update.
This field is coded as {TIME} and displays as 13/03/2025.
5
WORD Date & Time Manipulation: Tips & Techniques
6
WORD Date & Time Manipulation: Tips & Techniques
Note: This field uses a custom numeric picture switch to generate the appropriate
future, past or present tense.
7
WORD Date & Time Manipulation: Tips & Techniques
If, when the start date is the last day of the month, you need the end date to
be the last day of the month also even where the start month has less days
than the end month, you could use:
12-10-5053
Calculate a lease expiry date, using n months delay
12-10-5053
Calculate a day, date, month and year, using n days delay
12-10-5053
Note: To work with dates before 1 January 1901 or after 31 December 4095, see
”Working With Dates Before 1 Jan 1901 Or After 31 Dec 4095”, below.
Calculate a day, date, month and year, using n weeks delay
12-10-5053
Calculate the day & date of the last or nth-to-last day of this month
Thursday, 31 December 2009
Note: Increasing the ‘Subtract’ variable returns the nth-to last day of the month
(eg Subtract = 0 gives the last day of the month, while Subtract = 1 gives the
second-last day of the month). The field includes code to ensure that the
calculated day does not precede the 1st of the month.
Calculate the day & date of the nth day of next month
Thursday, 1 January 2009
Note: Increasing the ‘day’ variable returns the nth day of this month (eg day = 1
gives the 1st of the month, whilst day = 2 gives the 2nd of the month, and so
on). The field includes code to ensure that the calculated date does not
exceed the last day of next month.
Calculate the day & date of a given day of the week next month
Thursday, 1 January 2009
Note: The value in {SET Weekday 3} determines the day of the week to return
(0 = Mon, 1 = Tues, 2 = Wed, 3 = Thurs, 4 = Fri, 5 = Sat, 6 = Sun), while the
value in {SET Week 1} allows you to specify the week of the month. The ‘Limit’
variable is used to prevent the chosen date rolling over into the following
month. This also means that setting the Week variable to 5 will always return
the last instance of a given weekday occurring in a month.
To hard-code the field for a particular month, change the line
‘{SET Month {={DATE \@ M}+1}}’ to ‘{SET Month #}’, where # is the output
month. For example ‘{SET Month 3}’ will return a date in March.
Calculate the day & date of a given day of the week, n days in advance
The following field adds 90 days to today’s date, then finds the nearest
following 2nd or 4th Friday of the month. See the note from the previous topic
for details of how to set the weekday.
Thursday, 1 January 2009
8
WORD Date & Time Manipulation: Tips & Techniques
9
WORD Date & Time Manipulation: Tips & Techniques
10
WORD Date & Time Manipulation: Tips & Techniques
cells) in the formulae. The following table demonstrates how to do this using ASK fields to
create the bookmarks:
Calculate the # Years Months & Days Difference Between Two Dates
There are two approaches one might take to this issue, depending on whether you need an
approximation based on the number of days’ difference between two dates, or a more
accurate calendar-based approach that takes account of the actual days, months and years
involved.
The approximation below is based on a dividing the number of days difference between the
StartDate and the EndDate into a nominal number of 365.2425 day years (the average
length of a year using the Gregorian calendar) and converting the remainder into a
corresponding number of months and days, without regard to actual calendar months or leap
years:
1 Year, 5 Months, 28 Days.
In this field, if you need to include the StartDate (such as when calculating someone’s period
of employment), add 1 to the ‘Period’ value.
The equivalent calendar-based field, in which the calculation reflects the actual years months
and days, is:
1 Year, 5 Months, 29 Days.
In this field, if you need to include the StartDate (such as when calculating someone’s period
of employment), change the {SET sd {StartDate \@ d}} to {SET sd {={StartDate \@ d}-1}}.
Note: Both of the above fields get their inputs from the previous example.
11
WORD Date & Time Manipulation: Tips & Techniques
The period between 28 February 2000 and 29 March 2005, both dates exclusive, is more
than 5 years.
Note: To change the number of years (5 in the above example), change the 5 in the
50000 to suit. To incorporate months and days, you can change the 2 nd & 3rd digits to
match the month and the 4 th & 5th digits to match the day – you’ll want to change the
text for the output to suit also.
The following field extends the first of the above calculations to interactively accommodate a
user-defined period that can be expressed in years, months and days:
The period from 28 February 2000 to 29 March 2005 is less than 5 year(s), 1 month(s) and 2
day(s).
Note: You need to be careful with how the ends of months are interpreted. The
formulae interpret these literally (i.e. if one month ends on the 30 th and the other ends
on the 31st, for example, the field ignores that and tests the 30th against the 31st).
12
WORD Date & Time Manipulation: Tips & Techniques
13
WORD Date & Time Manipulation: Tips & Techniques
Instructions
Instruction 1
Instruction Text
Instruction 2
Instruction Text
As shown, each of the paragraphs can have their own styles, so that the instructions appear
properly formatted.
Show or hide instructions & graphics at print time
An alternative to the previous example would be to hide the field whenever the document is
printed, but allow it to be restored later. The following field does just that:
Your instructions go here.
To output something at print-time that isn’t ordinarily visible, you could reverse the field logic,
thus:
[] – the field code is between the brackets.
Pressing F9 or print preview restores the display. However, since the PRINTDATE field
doesn't support seconds, you might have to wait up to a minute before you can restore the
display.
Vary instructions & prompts, based on Print/Save Dates
A variation on the previous example could be to compare the PRINTDATE and SAVEDATE
or DATE values and to display different instructions depending on whether the document has
been printed since it was last saved or since it was opened, respectively. For example:
Not Printed Since Last Save
Vary text in a document, based on Print/Save Dates and the user’s name
The next field remains invisible until the document is printed, by way of a PRINTDATE test,
and only prints if the user's name (UserName) isn't "My Name". The UserName is whatever
appears under Tools|Options|User Information.
[] – the field code is between the brackets.
14
WORD Date & Time Manipulation: Tips & Techniques
The following field works with an input date in ‘ddmmyy’ or ‘dmmyy’ format.
Friday, 1 May 2009
Note: This last field only works with dates input as 5 or 6 digits, with 1 or 2 digits for
the day, 2 digits for the month and another 2 digits for the year. To make it work with
4-digit years, change the "00'-'00'-'00" string to "00'-'00'-'0000".
1
Actually, for compatibility purposes Excel replicated a bug in Lotus 123, which had day 1 as 1 January 1900
but wrongly treated that year as a leap year. Unless you’re using dates from before 1 March 1900, this can be
ignored.
15
WORD Date & Time Manipulation: Tips & Techniques
*
© macropod 2003-09
§
16