[英]Moment.js to display the year month day and hours
I am using the moment.js to display the Time in a specific formation -我正在使用 moment.js 以特定形式显示时间 -
var a = moment(new Date());
var b = moment(data.passwordLastSet);
var years = a.diff(b, 'year');
b.add(years, 'years');
var months = a.diff(b, 'months');
b.add(months, 'months');
var days = a.diff(b, 'days');
var hour = a.diff(b, 'hours')
b.add(hour, 'months');
this.passwordAgeYear = years;
this.passwordAge = years + ' years ' + months + ' months ' + days + ' days ' + hour + ' hours'
The above code display the date and time in below format上面的代码以下面的格式显示日期和时间
0 years 0 months 1 days 37 hours 0 年 0 月 1 天 37 小时
I have a requirement to display something like this我需要显示这样的东西
0 years 0 months 1 days 13 hours 0 年 0 月 1 天 13 小时
I have tried to use the fromNow and from我曾尝试使用 fromNow 和 from
https://momentjs.com/docs/#/displaying/from/ https://momentjs.com/docs/#/displaying/from/
But couldn't get the output.但无法获得输出。 How can I achieve this.
我怎样才能做到这一点。
I think your code have some an error, it should be like this,我认为你的代码有一些错误,应该是这样的,
var a = moment(new Date());
var b = moment(data.passwordLastSet);
var years = a.diff(b, 'year');
b.add(years, 'years');
var months = a.diff(b, 'months');
b.add(months, 'months');
var days = a.diff(b, 'days');
b.add(days, 'days');
var hour = a.diff(b, 'hours');
this.passwordAgeYear = years;
this.passwordAge = years + ' years ' + months + ' months ' + days + ' days ' + hour + ' hours';
In short, you forgot to add the days to b
, that is why you have days * 24 + hours
in hours.简而言之,您忘记将天数添加到
b
,这就是为什么您有days * 24 + hours
in hours 的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.