jquery years months and days variable



当系统出现时,我看到它非常复杂,但我有一个问题,我从哪里找到了这个代码,但我正在写,因为我没有太多知识,提前感谢那些帮助我的人。

http://jsfiddle.net/8qbjh3z2/

function dateAgo(date) {  
var startDate = new Date( date + " " + '12:00 AM');
var endDate = new Date();
var diffDate  = new Date(new Date() - startDate);
var yearDiff  = diffDate.toISOString().slice(0, 4) - 1970;
var monthDiff = diffDate.getMonth(); 
var dayDiff   = diffDate.getDate()-1;
var msec  = endDate - startDate;
var mins  = Math.floor(msec / 60000);
var hrs   = Math.floor(mins / 60);
var days  = Math.floor(hrs / 24);

var label = 'Posted ';
if( mins <= 30 ){
label += ' Just Now';
}else if( hrs <= 24 ){
label += ' Just Today';
}else{
if( yearDiff > 0  ){
label += ( yearDiff > 1 ) ? yearDiff+' years, ' : yearDiff+' year, ';
}
if( monthDiff > 0 ){
label += ( monthDiff > 1 ) ? monthDiff+' months, ' : monthDiff+' month, ';
}
if( dayDiff > 0 ){
label += ( dayDiff > 1 ) ? dayDiff+' days ' : dayDiff+' day ';
}
label += 'ago';
}

return label;   

}
(function() {
var elements = document.body.getElementsByClassName('iso-date');
for (var i = 0; i < elements.length; i++) {
var date  = elements[i].innerHTML;
var label = dateAgo(date);
elements[i].nextElementSibling.innerHTML = label;   
}
})();
<div class="iso-date">2019-09-30</div>
<div class="display-date"></div>

上述代码文件中的代码如下所示。我想要完成的系统会自动添加日期更改的月份、年份或日期。我想把周的部分作为额外的一部分添加进去。如果它是系统中看不到的东西,我希望将系统添加到我指定的类和id部分中。我将感谢你的帮助。

function getAge(dateString) {
var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate());
var yearNow = now.getYear();
var monthNow = now.getMonth();
var dateNow = now.getDate();
var dob = new Date(dateString.substring(6,10),
dateString.substring(0,2)-1,                   
dateString.substring(3,5)                  
);
var yearDob = dob.getYear();
var monthDob = dob.getMonth();
var dateDob = dob.getDate();
var age = {};
var ageString = "";
var yearString = "";
var monthString = "";
var dayString = "";

yearAge = yearNow - yearDob;
if (monthNow >= monthDob)
var monthAge = monthNow - monthDob;
else {
yearAge--;
var monthAge = 12 + monthNow -monthDob;
}
if (dateNow >= dateDob)
var dateAge = dateNow - dateDob;
else {
monthAge--;
var dateAge = 31 + dateNow - dateDob;
if (monthAge < 0) {
monthAge = 11;
yearAge--;
}
}
age = {
years: yearAge,
months: monthAge,
days: dateAge
};
if ( age.years > 1 ) yearString = " years";
else yearString = " year";
if ( age.months> 1 ) monthString = " months";
else monthString = " month";
if ( age.days > 1 ) dayString = " days";
else dayString = " day";

if ( (age.years > 0) && (age.months > 0) && (age.days > 0) )
ageString = age.years + yearString + ", " + age.months + monthString + ", and " + age.days + dayString + " old.";
else if ( (age.years == 0) && (age.months == 0) && (age.days > 0) );
else if ( (age.years > 0) && (age.months > 0))
ageString = age.years + yearString + " and " + age.months + monthString + " old.";

else if ( (age.years > 0) && (age.months == 0) && (age.days > 0) )
ageString = age.years + yearString ;
else if ( (age.years == 0) && (age.months > 0))
ageString = age.months + monthString + " old.";
else ageString = "Oops! Could not calculate age!";
return ageString;
}
document.getElementById('test').innerHTML += "<br />" + (getAge('02/09/2019'));
<div id="test">
</div>

有一个这样的解决方案,但我做不到。系统,例如:2年11个月,30天。给予。我希望系统中的注册日期是1年后的1年零2个月,所以我不能取消天数。

相关内容

  • 没有找到相关文章

最新更新