User:Yasir72.multan/test.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
Documentation for this user script can be added at User:Yasir72.multan/test. |
- Report page listing warnings and errors.
function getMonthDays(nmonth, nyear) {
switch (nmonth) {
case 0: case 2: case 4: case 6: case 7: case 9: case 11:
return 31;
case 3: case 5: case 8: case 10:
return 30;
case 1:
if (nyear%4==0)
return 29;
else
return 28;
default: return 0;
}
}
function getAgoTime(gdate, cdate) {
var daysago=0;
var monthago = 0;
var yearago=0;
if (cdate.getMonth()<gdate.getMonth()) {
yearago = cdate.getFullYear() - gdate.getFullYear()-1;
} else if
(cdate.getMonth()==gdate.getMonth()&&cdate.getDate()<gdate.getDate()) {
yearago = cdate.getFullYear() - gdate.getFullYear()-1;
} else {
yearago = cdate.getFullYear() - gdate.getFullYear();
}
if (cdate.getMonth()<=gdate.getMonth()&&cdate.getDate()<gdate.getDate()) {
monthago = cdate.getMonth()-gdate.getMonth()+12-1;
} else if (cdate.getMonth()>gdate.getMonth()&&cdate.getDate()<gdate.getDate()) {
monthago = cdate.getMonth()-gdate.getMonth()-1;
} else if (cdate.getMonth()<gdate.getMonth()) {
monthago = cdate.getMonth()-gdate.getMonth()+12;
} else {
monthago = cdate.getMonth()-gdate.getMonth();
}
if (cdate.getDate()>=gdate.getDate()) {
daysago = cdate.getDate() - gdate.getDate();
} else {
daysago = cdate.getDate()-gdate.getDate()+getMonthDays(gdate.getMonth(),gdate.getYear());
}
var agostr = "";
if (yearago==0&&monthago==0&&daysago==0) {
agostr = "Today";
} else {
if (yearago>1)
agostr += yearago + " years ";
else if (yearago==1)
agostr += yearago + " year ";
if (monthago>1)
agostr += monthago+ " months ";
else if (monthago==1)
agostr += monthago + " month ";
if (daysago>1)
agostr += daysago + " days ";
else if (daysago==1)
agostr += daysago + " day ";
agostr += "ago";
}
return agostr;
}
var gdate = new Date("28 February 2019");
var cdate = new Date ("27 March 2019");
alert(getAgoTime(gdate, cdate));