On some websites, we often see that the newly posted messages are displayed as "just", which takes a long time. They are displayed as "a few days ago", "a few hours ago", and so on, how can this be implemented? In fact, there is no need to format these items in the database, and they can be displayed on the client. There are a lot of JS writes on the Internet, so now they are closed into a jquery plug-in,CodeAs follows:
VaR Datetimestamp = varparam? Date. parse (varparam. Replace (/-/GI ,"/")): New Date (). gettime ();
VaR Now = New Date (). gettime ();
VaR Diffvalue = now-datetimestamp;
If (Diffvalue <0 ){
Alert ("the end date cannot be earlier than the start date! ");
}
VaR Monthc = diffvalue/month;
VaR Weekc = diffvalue/(7 * day );
VaR Dayc = diffvalue/day;
VaR Hourc = diffvalue/hour;
VaR Minc = diffvalue/minute;
If (Monthc> = 1 ){
Return Varparam;
}
Else If (Weekc> = 1 ){
Result = "posted on" + parseint (weekc) + "weeks ago ";
}
Else If (Dayc> = 1 ){
Result = "posted on" + parseint (dayc) + "Days Ago ";
}
Else If (Hourc> = 1 ){
Result = "posted on" + parseint (hourc) + "Hours Ago ";
}
Else If (Minc> = 1 ){
Result = "posted on" + parseint (minc) + "Minutes Ago ";
} Else
Result = "just published ";
ReturnResult;
This is the main Code. For specific files, you can download the attachment: Download