Question Thembelani Mlalazi · Dec 5, 2017

How to Subtract or Add dates

I am trying to age based on a given date and current date here is my code:


Property DOB As %Date
Method GetAge() As %Integer
{
   if (..DOB="")
    {
  set today=0 
    }
   else
  {
  set today=$ZDate($HOROLOG,2)-$ZDate(..DOB)
  }
  write "Today's==="_$ZDate($HOROLOG),!
  write today
  return today
}

Comments

Vitaliy Serdtsev  Dec 5, 2017 to Dmitry Maslennikov

In this case $system.SQL.DATEDIFF is not suitable:

USER><FONT COLOR="#0000ff">w $system</FONT><FONT COLOR="#008080">.SQL</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">DATEDIFF</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"yy"</FONT><FONT COLOR="#000000">, </FONT><FONT COLOR="#0000ff">$zdh</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"30.12.1990"</FONT><FONT COLOR="#000000">,4), </FONT><FONT COLOR="#0000ff">$zdh</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"01.01.1991"</FONT><FONT COLOR="#000000">,4))</FONT>
1

USER><FONT COLOR="#0000ff">w </FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$ZD</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$zdh</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"01.01.1991"</FONT><FONT COLOR="#000000">,4),8)-</FONT><FONT COLOR="#0000ff">$ZD</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$zdh</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"30.12.1990"</FONT><FONT COLOR="#000000">,4),8))\10000</FONT> 0

0
Vitaliy Serdtsev · Dec 5, 2017

See Sample.Person in "SAMPLES".

<FONT COLOR="#000080">/// Person's age.<br>
/// This is a calculated field whose value is derived from <property>DOB</property>.
Property </FONT><FONT COLOR="#000000">Age </FONT><FONT COLOR="#000080">As %Integer </FONT><FONT COLOR="#000000">[ </FONT><FONT COLOR="#000080">Calculated</FONT><FONT COLOR="#000000">, </FONT><FONT COLOR="#000080">SqlComputeCode </FONT><FONT COLOR="#000000">= { </FONT><FONT COLOR="#0000ff">Set </FONT><FONT COLOR="#800080">{Age}</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">Sample.Person</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">CurrentAge</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800080">{DOB}</FONT><FONT COLOR="#000000">)}, </FONT><FONT COLOR="#000080">SqlComputed</FONT><FONT COLOR="#000000">, </FONT><FONT COLOR="#000080">SqlComputeOnChange </FONT><FONT COLOR="#000000">= DOB ];</FONT>

<FONT COLOR="#000080">/// This class method calculates a current age given a date of birth <var>date</var>. ClassMethod </FONT><FONT COLOR="#000000">CurrentAge(</FONT><FONT COLOR="#ff00ff">date </FONT><FONT COLOR="#000080">As %Date </FONT><FONT COLOR="#000000">= </FONT><FONT COLOR="#800080">""</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#000080">As %Integer </FONT><FONT COLOR="#000000">[ </FONT><FONT COLOR="#000080">CodeMode </FONT><FONT COLOR="#000000">= expression ] { </FONT><FONT COLOR="#0000ff">$Select</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">date</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#008000">""</FONT><FONT COLOR="#000000">:</FONT><FONT COLOR="#008000">""</FONT><FONT COLOR="#000000">,1:(</FONT><FONT COLOR="#0000ff">$ZD</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$H</FONT><FONT COLOR="#000000">,8)-</FONT><FONT COLOR="#0000ff">$ZD</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">date</FONT><FONT COLOR="#000000">,8)\10000)) }</FONT>

0
Mike.W · Dec 8, 2017

I won't embarrass myself by listing the MUMPS code from 1991 that does this on our application, but I will comment that you need to work out how many birthdays have gone by so it must compare month and day values once the basic year subtraction has been done. It gets quite complicated. (Might also like to look at whether you need more than just a "years old", and also need months or days for very low values.)

Mike

0