Reputation: 23
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
Upvotes: 0
Views: 1140
Reputation: 1037
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();
Upvotes: 0
Reputation: 6418
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();
Upvotes: 1
Reputation: 2156
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...
Upvotes: 1