Display date difference in MS Word field
Asked Answered
L

5

9

I have an CV and would like to automatically update my age when opening. So, what formula should I insert in a MS Word field?

Something like:

{= {DATE} - {"01/01/1983"} }
Lyontine answered 27/4, 2010 at 14:14 Comment(4)
to those who voted to close - Word field can very much be programming full of variables and if/then statements. voting to close a question like this means you'd also vote to close every single excel-formula tagged question on SO. check out the link attached and you'll see :)Bluejacket
@Otaku - despite being one of those voting to close I agree with you to some extent. There is a grey area here between being a "power user" of these application and it being a programming question. I think we have to take each question on it's own merits. In this case I came down on the side of migration, in another case I might not.Wavellite
@ChrisF: I could add "NotProgrammingRelated" tag, but... this would not be true.Lyontine
@ChrisF: Fair enough. My rationale is that anything that requires a user to do their own conditional calculations with code is programming related, everything else is "power user". For example: Conditional Formatting in Excel is "power user" (unless Formula is chosen) as it is GUI-driven. Same in Word, for example, if you use the Fields dialog to enter a field - but if you need to programmatically manipulate the results, I'd call it programming. I know of very, very few "power users" who can code anything at all.Bluejacket
B
4

This is one of the better sites for working with Word field codes - http://www.addbalance.com/usersguide/fields.htm - and there are examples there on how to create +/- dates.

Here's how you would calculate based of a birth month of January and birth year of 1983.

{IF{DATE\@"MM"} >= 01 { = {DATE\@"YYYY"} – 1983 \#"00"} { = {DATE\@"YYYY"} – 1984 \#"00"}}

Note that in the case of the month of January, this IF statement will always be true and give you =YEAR-1983. You can use another nested IF statement to check on the day to further get more accurate.

Bluejacket answered 27/4, 2010 at 23:28 Comment(5)
I saw this, however not found there an answer to my question. only Today\Create\Save date are available.Lyontine
Yeah, you have do use DATE to do this calculation. Word field codes are notoriously difficult to type in and get right as even the wrong spacing will give different results. Code you need is updated in the answer.Bluejacket
@serhio: you forgot the closing bracket } at the end. When you enter field codes, you can't just type in the brackets though - you have to use Ctrl+F9 to enter the brackets.Bluejacket
Thanks! <kbd>Ctrl+F9</kbd> was useful.Lyontine
Where do I put that formula?Anzio
A
4

I tried but could not find in Word fields the equivalent of the Excel way to substract a daytime from another one (let’s say 05/18/1983 from today 03/21/2013) which is easy in Excel since a daytime (01/01/1900 in Excel 2013) has been chosen as the beginning and every other day since is calculated according to this reference, the shown details only being a way of representing this result (full date, month in figures or its full name, or even the simple number of Excel), which can be toggled by right clicking on the cell and choosing the category cell format (since 01/01/1900 is 1, then 05/18/1983 is 30454 (...and also 00/01/1900 is 0!)).

To answer the initial question, I suggest this writing:

{= {DATE  \@ "YYYY"} - my_year - 1*{=OR({={DATE  \@ "MM"} < my_month};{=AND({={DATE  \@ "MM"} = my_month};{={ DATE  \@ "dd"} < my_day})})}  \* MERGEFORMAT}

bearing in mind:

  1. that curly brackets {} are Word specifics symbols (not normal ‘letters’) and should be inserted by pressing CTRL+F9.
  2. 'my_year', 'my_month' (twice) and 'my_day': should be numbers
  3. this writing is quite short, and suggest only 4 changes (unfortunaletly the month twice), which would help preventing errors
  4. for the beginners, this writing says: today's year minus my_year, and substract one if either today's month value is lower than my_month or both is equal and today's day value is lower than my_day, so that we do not have added one year in excess.

The MERGEFORMAT option is useful to keep the correct format.

Also, for those who are not used to working with Word fields:

  • right clicking on the text inside curly brackets {} and choosing “Toggle Field Codes”, switches from the code itself (texte inside) or the result of the calculus of this code, but that may not be up to date (may be a previous result also the code has been changed).
  • to make sure the result are up to date, right click and choose “Update Field”.
  • and to make sure you open every field inside a master field, select the whole field (better select the whole line containing it), right click and choose “Toggle Field Codes” (otherwise, some fields that are badly coded or that may return a void expression, may not be seen and may affect the seeing result later on when not wanted).

That would do for 05/18/1983 (I changed 1st of January, which is 01/01 to 18th of avoid confusion):

{= {DATE  \@ "YYYY"} - 1983 - 1*{=OR({={DATE  \@ "MM"} < 05};{=AND({={DATE  \@ "MM"} = 05};{={ DATE  \@ "dd"} < 18})})}  \* MERGEFORMAT}
Astyanax answered 21/3, 2013 at 13:15 Comment(1)
Upvote specifically for // 1. that curly brackets {} are Word specifics symbols (not normal ‘letters’) and should be inserted by pressing CTRL+F9 // Thanks !Intervention
T
1

I would suggest calculating a serial number for both dates and subtracting them from each other. It doesn't have to be too involved: we're only interested in the number of years anyway. year*365 + month*31 + day should do nicely here.

For the number of years divide the difference by 365. Then strip the fractional part using INT (not ROUND which would make you 1 year older half of the year).

{ = INT(({ DATE \@"YYYY" }*365 + { DATE \@"MM" }*31 + { DATE \@"dd" } 
      - (my_year*365 + my_month*31 + my_day)) / 365) \# "0" }
Theatricalize answered 13/6, 2017 at 12:56 Comment(0)
N
0
  1. Create the text form field. Name the field as "BirthDate"

enter image description here

  1. Create the field with the following code:

{={DATE \@ "yyyy"} — {BirthDate \@ "yyyy"}}

Natashianatassia answered 29/9, 2017 at 12:44 Comment(1)
How can I replace BirthDate with a constant? What date format is accepted for the constant?Anzio
I
0

I've done something like this:

10.2014 – 05.2020 (date1 - date2)
5 years, 8 months (difference)

I've mapped "10.2014" as bookmark (insert > bookmark) with the name "d1" (it will come in handy later. "05.2020" is the "Date" (insert > field > Date) formatted as {DATE \@ "MM.yyyy"}. To access this, RMB > Toggle Field Codes

The next step is compute the "5 years, 8 months" difference. For this, I've computed separately the "years" and "months" numbers, with two different fields, as follows: field1 years, field2 months where

field1 = {= Round ((20*365+5*30 - 14*365-10*30)/365-0.4999,0}
field2 = {=(({Date \@""yy"}-{d1 \@"yy"}) - Round(({Date\@""yy"}*365 + {Date\@""M"}*30 - {d1 \@"yy"}*365 - {d1 \@"M"}*30)/365 -0.4999,0)*12 + ({Date \@ "M"} - {d1 \@ "M"}) + 1)

It can be further tweaked to show "year" or "years", "month" or "months" , but for me was enough.

Some further explanations:

  • Do not write {}. Use Ctrl+F9 to insert a field
  • field2 complications were done to solve the problem for differences between 10.2015 and 02.2020
Institutor answered 7/5, 2020 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.