0

From my backend I get an object with date of birth that is a long value.For frontend I use angular 4 (typescript) I would like to extract year from that date of birth to be able to calculate age but I have no idea how to parse long to some kind of Date object in typescript. Could you give me a hint where to look for information?

Maybe something along lines of:

a: Number;
let a = new Date(762861060).getFullYear();

Thank you for help

3 Answers3

1

You can try simple code like this:

var birthDate = new Date(762861060);
var todayDate = new Date();    
var milliDay = 1000 * 60 * 60 * 24; // a day in milliseconds;

var ageInDays = (todayDate - birthDate) / milliDay;   
var ageInYears =  Math.floor(ageInDays / 365 );

console.log(ageInYears)

See more related answers on this and this questions...

Matheus Cuba
  • 2,068
  • 1
  • 20
  • 31
1

You're not far off, if you don't need fractional years:

var birthDate = new Date(762861060);
var laterDate = new Date();  
var yearsBetween = laterDate.getFullYear() - birthDate.getFullYear();
Robert Penner
  • 6,148
  • 1
  • 17
  • 18
0

You can use Moment.js:

import * as moment from 'moment';

var diff = moment.duration((moment(762861060)).diff(moment()));
var age_in_days = diff.days();
var age_in_years = diff.years();
Nedzad G
  • 1,007
  • 1
  • 10
  • 21