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-2018 by Barry MacDonnell. All Rights Reserved.

Page updated Dec 21, 2017

TXT2DATE - Numerical text to date converter

Download TXT2DATE.ZIP (v2.06, 07/31/17; and v1.05, 9/18/01; ZIP file = 37,783 bytes)


(See note at bottom of next column about v1.05.)

Compatible with WordPerfect 8 and later versions

WordPerfect 11 users: See important information about using macros in the first release of WP11 (11.0.0.233) at the top of this page.

Downloading, Documentation, Modifications, and Support


Related pages -

•  DATEREPL - Change all date codes (dynamic dates) to text (static dates)

•  DATEINS - Quickly insert any calendar-selected date into your document in various formats (including "legal-style" dates)


Txt2Date.wcm - "Text to Date" - A WordPerfect macro to convert all numerical dates — i.e., text strings (NOT date codes) in the format "01/01/17," "1/1/17," "1/1/2017," or similar — to static text strings in a format such as "January 1, 2017," "1 Jan 17," "the 1st of January, 2017," etc. (For a macro to use the current (system) date as a static text date see Footnote 1.)

Operation:

Any one of 8 conversion formats can be chosen from a menu [screen shot], including "legal" dates such as "the 31st day of January, 2017."

Using the menu's options, the macro can convert just the item under the cursor or it can convert all items; for the latter, it can start at the current cursor location instead of at the top of the document and/or it can pause at each item for confirmation.

Note that dates in the format MM/DD/YY from 01/01/00 to 12/31/29 are converted to dates between 2000-2029. Dates in the format MM/DD/YYYY will be converted to the specified year. (E.g., 1/1/00 will convert to January 1, 2000, while 1/1/1900 will convert to January 1, 1900.)

New in v2.06: For those documents that use numeric dates in the format DD/MM/YY rather than MM/DD/YY, you can swap the values assigned to the day and month with a simple setting in the User Modification Area. (This is a mutually exclusive setting, so if you want to use both settings, depending on the document, just make a (renamed) copy of the macro with different settings in the Modification Area.)

Notes and tips:

•  The numeric date components are assumed to be separated by forward slashes ("/"); any other character will cause the macro to fail.

•  The macro's pop-up menu can be suppressed for speed, and/or other menu defaults can be set by making simple changes to the User Modification Area at the top of the macro's code. Please read the comments at the top for more information.

•  If the suffix ("st", "nd", "rd", "th") is not superscripted, it may be due to Tools, QuickCorrect, "Replace words as you type" (on the QuickCorrect tab) is disabled (un-ticked). (Sometimes the program disables it automatically.) Hence, if you want them to be superscripted you need to enable that option. Also be sure that "Quick ordinals" on the Format-As-You-Go tab is enabled, too. (For a macro to enable QuickCorrect and optionally set any of several other QuickCorrect options, see QC.wcm here.)

•  A simplified (and alternative) version of this macro that just inserts a legal-style date based on the current date can be seen in Footnote 1.

- - -
Note: An original version (1.05) is also included (Txt2Date105.wcm) in the download file as a programmer's reference to using floating cells to convert text strings. However, v1.05 does not work reliably under WP8 (sorry). Just delete the older version if you don't need it.


Footnote 1

What if you just want to insert today's date as (e.g.) "the 16th of December, 2017" (which is the current date as I write this) as a static text date in a specific, typed format?
 
Below is some sample macro code that does the job. The code inserts the current date as a legal-style date.

As written, it does this in three locations by locating pre-existing bookmarks you created (see Tools, Bookmark on your program menu) and then typing the legal-style date at those bookmarked locations. (To modify the macro to do it in just one location see the comments at the top of the code.)

To copy this code into your WordPerfect program to create a working macro, see here.

// Macro begins:

// Purpose:
// Inserts the current date as a legal-style date such as
// "the 15th of July, 2019" (change the Type commands to add
// other text, such as "the 15th day of July, 2019").
//
// In this code snippet, it will do this at 3 previously bookmarked
// locations, but it can be modified to be used for any number of locations
// by adding or omitting the OnError()..Label() segments below.
// The bookmarks used here are named Date1, Date2, and Date3.

// Tip:
// You don't need to use bookmarks if the cursor is already where it should be.
// In that case, just use a single instance of the Type() command and remove
// the OnError/BookmarkBlock/Label commands from the 3 segments in the
// the code below. Thus, just one legal-style date will be inserted at that location.

// Notes:
// •  The specified bookmarks must exist; if any do not, their locations will be
// skipped. (No harm is done, other than dates not being typed.)
//
•  The bookmarks can be single codes or double-paired codes; the latter allow
// you to insert your own text placeholders in the document between the paired codes
// (e.g., "[Date Contract Begins]"), which will be replaced when the macro executes
// the BookmarkBlock() commands below. Important: The bookmark name must be
// exactly the same (including any square brackets if used) as in the macro code.
// This method can be helpful in documents that might be edited in the future
// by visibly identifying bookmark locations.

// Main macro code:

// FIRST: Get the current day, month, and year from the computer system:
vDay:=StrTrim(?DateDay;;TrimLeft!;"0")    // (Remove any leading zeros in day)
vMonth:=?DateMonth
vYear:=?DateYear

// Type (as text) the date in "legal-style" at the bookmarked locations:
OnError(NoDate1)
BookmarkBlock ("Date1")
Type("the "+fAbbrevDay(vDay)+" of "+fMonthName(vMonth)+", "+vYear)
Label(NoDate1)

OnError(NoDate2)
BookmarkBlock ("Date2")
Type("the "+fAbbrevDay(vDay)+" of "+fMonthName(vMonth)+", "+vYear)
Label(NoDate2)

OnError(NoDate3)
BookmarkBlock ("Date3")
Type("the "+fAbbrevDay(vDay)+" of "+fMonthName(vMonth)+", "+vYear)
Label(NoDate3)

Return  // (<- exits from the macro)

// (Original function code snippets by John Land.)
// Gets the "in (month) string" from the Type() command above:
Function fMonthName (vInStr)
    vMonthList[]={
    "January";"February";"March";"April";"May";"June";"July";"August";
    "September";"October";"November";"December"}
    // Gets the "in (day) string" from the Type() command above:
    Return (vMonthList[vInStr]) 
EndFunc
Function fAbbrevDay (vInDay)
    Switch (vInDay)
        CaseOf "01";"1";"21";"31":    vOutDay=vInDay+"st"
        CaseOf "02";"2";"22":        vOutDay=vInDay+"nd"
        CaseOf "03";"3";"23":        vOutDay=vInDay+"rd"
        Default:            vOutDay=vInDay+"th"
    EndSwitch
    Return (vOutDay)  // Returns the proper format (e.g., "31st")
EndFunc

// Macro ends here

Tips:

•  For some tasks this code might work best as a template macro triggered by the Post New trigger (triggers are briefly explained here). It would then automatically type the legal-style date in various pre-determined locations as soon as you load a document based on that template.

•  If the suffix ("st", "nd", "rd", "th") is not superscripted, it may be due to Tools, QuickCorrect, "Replace words as you type" (on the QuickCorrect tab) is disabled (un-ticked). (Sometimes the program disables it automatically.)  Hence, if you want them to be superscripted you need to enable that option. Also be sure that "Quick ordinals" on the Format-As-You-Go tab is enabled, too. (For a macro to enable QuickCorrect and optionally set any of several other QuickCorrect options, see QC.wcm here.)

Explanation of how the Function commands work here (for the curious):


Let's just look at the Function fAbbrevDay (vInDay) segment above.

Suppose today is the 22nd of the month. Here's how the Function works:

The Type(...) command's parameter (above) includes the function named fAbbrevDay(vDay) -- which in our example macro uses vDay, a variable that stored the day of the month (received from the computer's system date -- see the first operational line of the macro).

Hence, if today is the 22nd of the month, fAbbrevDay(vDay) will now be fAbbrevDay("22").

The "22" part is immediately passed down as input to the Function segment in this command as vInDay: Function fAbbrevDay (vInDay). This will now be Function fAbbrevDay ("22") -- i.e., the function's name and the input to the function.

At this point the function's work needs to be done. In our example the function includes a Switch command to (e.g.) change any "22" to "22nd" and stores it -- plus a text suffix --in the vOutDay variable.

The Return (vOutDay) output command now represents Return ("22nd") -- whereupon this output (the "22nd" part) is sent back up to the Type() command, where it is rendered as "22nd" there.

Fortunately, all this happens virtually instantaneously. :-)

[The segment, Function fMonthName (vInStr), works similarly.]