Skip to content

How can I calculate age in X years, X months & X days #3030

Answered by tan75
nitin2953 asked this question in General
Discussion options

You must be logged in to vote

Hello,

is this the one?

import { formatDuration, intervalToDuration } from "date-fns";

const startDate = new Date(2003, 4, 29);
const endDate = new Date();

const duration = intervalToDuration({ start: startDate, end: endDate });

const age = formatDuration(duration, { format: ["years", "months", "days"] });

console.log(age); //=> '18 years 11 months 12 days'


Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@nitin2953
Comment options

@nitin2953
Comment options

Answer selected by nitin2953
Comment options

You must be logged in to vote
1 reply
@nitin2953
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants