Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

javascript - 计算并显示年,月和日的人的年龄(Calculate and show age of a person from year, month and day)

I got this as JSON data from the API and need to calculate the age:(我是从API获得的JSON数据,需要计算年龄:)

{"fullname":"Nikita","email":"[email protected]","city":"London","mobile":"08888888888","birthday"{"year":1980,"month":7,"day":23}} Currently, I'm showing the date of birth as {{anon.birthday}} which results in 1980-7-23 .(目前,我将出生日期显示为{{anon.birthday}} ,结果为1980-7-23 。) I would like to show the age instead.(我想显示年龄。) How can I go about doing this?(我该怎么做呢?)   ask by Nikita Gupta translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use moment.js npm i moment(您可以在npm i moment使用moment.js npm i moment)

import * as moment from 'moment'; // Get the date of birth as a moment const birthDateMoment: moment.Moment = moment(`${yourObject.birthday.day}/${yourObject.birthday.month}/${yourObject.birthday.year}, 'DD/MM/YYYY'); Option1: using duration : Get the duration time between now (moment()) and the birthDate(选项1:使用duration :获取从现在(moment())到birthDate之间的持续时间) const durationLife = moment.duration(moment().diff(birthDateMoment)) durationLife.years(); Option 2: Use diff(选项2:使用差异) moment().diff(birthDateMoment, 'years'); Option 3: time from(选项3:开始时间) birthDateMoment.from(moment());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...