Home | Tips | Library | Other Authors | Other WP Sites | Writer's Links | Contact | Site Map | Donate

Barry MacDonnell's
Toolbox for WordPerfect

Macros, tips, and templates for Corel® WordPerfect® for Windows®
© Copyright 1996-2024 by Barry MacDonnell. All Rights Reserved.

Page updated Feb 23, 2023

WordPerfect Tips
Main tips page | Browse more tips
Need to calculate a new date from today's date, or from some other date? Here a several methods.

There are several ways to do this, depending on what you want as output.

•  You can use a WordPerfect table to calculate the new date:

Assuming the first date should be in table cell A1 ...

[1] Format cell A1 as a Date by right-clicking the cell, choosing Numeric Format, then Date/Time. (You can click Custom to format the date differently, if desired.)
 
[2] Click OK. In the table cell, enter a date such as 12/4/18 and when you tab to the next cell or click in another cell, the date will appear as a date-formatted string.

Now, assuming you want to calculate a date 90 days hence:

[3] In the second ("new date") cell, format it as a Date, and then enter this formula in the cell:

+A1+90

-----
Note

The table must be set to auto-calculate:

With the cursor in the table, click the top menu: Table, Calculate. You should see the Calculate dialog, which lets you turn automatic calculation on and off.
Also, make sure you have not turned calculation off in the cell itself (right-click the cell, then choose Format, then un-check the "Ignore cell when calculating" box).


•  You can use a WordPerfect floating cell to calculate the new date and insert it at the cursor location.

See Footnote 1 below for the manual procedure and also some macros that can create the floating cell with the formula already in it, and then insert it in your document -- all in one operation.

•  You can use a WordPerfect macro from this site to calculate (and optionally insert) a new date that is "x" days in the future (or the past) from today (or other date).

See this small downloadable macro, Insert new date in document.wcm, included in the archive file InsDate.zip. It asks you for a date (the current date is the default), the number of days (plus or minus) from today, and some basic format options for the date. Then it displays the new date in a small dialog, which lets you insert the new date in the document at the current cursor location.

•  To find the number of days between two dates,

- see: Footnote 2.
Or:
- see: Charles Rossiter's Julian macro.

•  To find the day of the week a given date will fall on, try the Find day of week macro.
Roy "lemoto" Lewis has posted several macros that calculate dates, such as Days After and Due Date.


Notes section

Footnote 1

•   Here is the manual procedure to create a date-calculating floating table cell (mentioned above):

1. Create a floating cell at the cursor location: On the top menu (assuming you are using a WordPerfect menu), click Table, Create, <Floating cell radio button>, Create. In Reveal Codes you should see [FltCell><FltCell], where the red block indicates the current insertion cursor position.

2. In the Table formula bar that should be visible above the document area, click inside the blank field (to the right of the blue checkmark), and type this formula into that field:

+DATETEXT(DATEVALUE(DATE())-1)
    Or:
+DATETEXT(MDY()-1)

3. Click the blue check mark to accept the formula and insert it in the floating cell. The calculated date should appear between the pair of [FltCell] codes in the document, as a text string.

4. Click Close on the formula bar. The [FltCell] codes can be deleted in Reveal Codes, since they have done their job of creating the static date.

•   Here are a few small macro examples you can copy into your WordPerfect program.

Each macro calculates "x" days from either the current (system) date or from another, specified date. They will then insert that new date in the document as a (static) text string inside a floating cell.

Note:

Floating cells cannot be created inside another table; hence, the initial code segment in each example that pops up a warning message.

Examples #3 and #4 might be good candidates for a template macro that can be triggered automatically. For more on how to automate a template, see the main Tips page, here.

Tips:

☼  The resulting floating cell (i.e., everything bracketed by [Flt Cell] codes, and including those codes) can be copied elsewhere in the document.

☼  You can add a DeleteCharPrevious command after the FloatingCellFormula command, as was done below (but disabled with comment marks ("//")). This will delete the [FltCell] codes as part of a "clean up" process.


Example #1 - Use the current date, calculate a new date (+/-) "x" days from that date, based on a number of days the user inputs into a small dialog.

// Macro code begins:

If(?InTable)
Messagebox(; "Error";
  "You cannot play this macro inside a Table.")
  Return
Endif

OnCancel(End@)  // (used in case the user exists the GetNumber dialog)

vDate:= DateString()   // (stores today's date)

GetNumber(vDays;
"How many days from today?"+NToC(0F90Ah)+
"(use a minus sign for past days)";
"Insert a Date +/- 'x' Days from Today")

FloatingCellCreate
FloatingCellFormula("+DATETEXT(DATEVALUE("""+vDate+""")+"+vDays+")")
// DeleteCharPrevious // (optionally delete floating cell codes)

Label(End@)
Return
// End of macro


Example #2 - Use a static date other than the current date, calculate a new date (+/-) "x" days from that date, based on a number of days the user inputs into a small dialog.

// Macro code begins:

If(?InTable)
Messagebox(; "Error";
  "You cannot play this macro inside a Table.")
  Return
Endif

OnCancel(End@)  // (used in case the user exists the GenNumber dialog)

// Example:
// Store "January 15, 2023" as a static date (Day;Month;Year) in vDate.
// (Long! spells out the date in the dialog)
vDate:=DateString(DateAndTime(15;1;2023); Long!)

GetNumber(vDays;
"How many days from " +vDate+ " ?" +NToC(0F90Ah)+
"(use a minus sign for past days)";
"Insert a Date +/- 'x' Days from Today")

FloatingCellCreate
FloatingCellFormula("+DATETEXT(DATEVALUE("""+vDate+""")+"+vDays+")")
// DeleteCharPrevious // (optionally delete floating cell codes)

Label(End@)
Return
// End of macro


Example #3 - Use the current date, calculate a new date that is a specified number of days from that date.

// Macro code begins:

If(?InTable)
Messagebox(; "Error";
  "You cannot play this macro inside a Table.")
  Return
Endif

vDate:= DateString()   // (stores today's date)
vDays:= -90   // (e.g., minus 90 days)

FloatingCellCreate
FloatingCellFormula("+DATETEXT(DATEVALUE("""+vDate+""")+"+vDays+")")
// DeleteCharPrevious // (optionally delete floating cell codes)

Return
// End of macro


Example #4 - Use the current date, calculate a new date that is a specified number of days from that date.

(Similar to Example #3.) Note that in Example #3 you could remove the vDate and vDays commands, and use just the following FloatingCellFormula() command, which is the equivalent to them. It will calculate a new date that is minus 90 days (in this example) from the current date (MDY()). So this example is merely an alternattive to Example #3:

// Macro code begins:

If(?InTable)
 Messagebox(; "Error";
 "You cannot play this macro inside a Table.")
 Return
Endif

FloatingCellCreate
FloatingCellFormula("+DATETEXT(MDY()-90)")
// DeleteCharPrevious // (optionally delete floating cell codes)

Return
// End of macro

 


Footnote 2

Unlike the macros in Footnote 1 above, which use floating table cells, this macro calculates the difference between a target date and the current date using a pair of PerfectScript DateAndTime commands.

Just change (and save) the three Target date variables (vTD, vTM, vTY) to suit your own needs and then play the macro.

The three Current date variables (vCD,vCM,vCY) in this example are taken from WordPerfect system variables (but could just as well be typed into the command instead, similar to the way it was done with the Target date).

To copy this macro code into your WordPerfect program to produce a functioning macro see here.

Example #5:

// Macro code begins:

// Set the Target (future) day, month, year:
vTD:=6 // (target day, 1-31)
vTM:=7 // (target month, 1-12)
vTY:=2021 // (target year, YYYY)

// Set the Current day, month, year (here, via WP system variables):
vCD:=?DateDay
vCM:=?DateMonth
vCY:=?DateYear

// Validate the Target date (optional but typically helpful):
vDays:=DateDaysInMonth (Month: vTM; Year: vTY)
If(vTD<1 OR vTD>vDays)
 Messagebox(;"Error";
    "The Target DAY was set to an invalid day for the Target month: "+vTD)
    Quit
Endif
If(vTM<1 or vTM>12)
    Messagebox(;"Error";
    "The Target MONTH was set to an invalid number: "+vTM)
     Quit
Endif
If(vTY<1601 or vTY>4000)
    Messagebox(;"Error";
    "The Target YEAR was set to an invalid number: "+vTY)
    Quit
Endif

// Compute the difference, convert to an integer, and display the result:
vDiff:=DateAndTime(vTD;vTM;vTY)-DateAndTime(vCD;vCM;vCY)
vDiff:=ConvertType(vDiff;Integer!)
MessageBox(;"Difference between the target date and today:"; vDiff+" days.")

// Optional: Do something with the difference; e.g., display a message:
If(vDiff <= 0)
    Messagebox(;;"The Target Date has passed.")
Else
    Messagebox(;"" ;"The Target Date has NOT passed.")
    // ... continue with macro ... (e.g., use Go() to jump to a Label() ) ...
Endif

Return
// End of macro