On Sunday, 15 January 2017 at 03:43:32 UTC, Nestor wrote:
Hi,

I would simply like to get someone's age, but I am a little lost with time and date functions. I can already get the duration, but after reading the documentation it's unclear to me how to convert that into years. See following code:

import std.stdio;

void getAge(int yyyy, int mm, int dd) {
  import std.datetime;
  SysTime t1 = SysTime(Date(yyyy, mm, dd));
  SysTime t2 = Clock.currTime();
  writeln(t2 - t1);
}

int main() {
  try
    getAge(1980, 1, 1);
  catch(Exception e) {
    writefln("%s.\n(%s, line %s)", e.msg, e.file, e.line);
  }
}

Notice getAge should return ubyte instead of void, only I haven't been able to find how to do it. Any suggestion would be welcome.

Thanks in advance.

Does this do what you want?
import std.stdio;

uint getAge(int yyyy, int mm, int dd) {
  import std.datetime;
  SysTime t1 = SysTime(Date(yyyy, mm, dd));
  SysTime t2 = Clock.currTime();
  return( (t2.year - t1.year));
}

void main() {
  auto age = getAge(1980, 1, 1);
  writefln("age is %s", age);
}

Reply via email to