<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <TITLE> New Document &L t;/title> <meta name= "generator" content= "EditPlus" > <meta name= "Author" content= "" > <meta NAME= "Key Words "content=" "> <meta name=" Description "content=" "> <script language=" javascript "> function stringt Odatetime (postdate) {var second = 1000; var minutes = second*60; var hours = minutes*60; var days = hours*24; var months = days*30; var twomonths = days*365; var mydate = new Date (Date.parse (postdate)); if (isNaN (mydate)) {mydate =new Date (Postdate.replace (/-/g, "/")); var nowtime = new Date (); var longtime =nowtime.gettime ()-mydate.gettime (); var showtime = 0; if (longtime > months*2) {return postdate; else if (longtime > months) {return "1 months ago"; else if (longtime > Days*7) {return ("1 weeks ago"); else if (longtime > days) {return (Math.floor (longtime/days) + "day Ago"); } elseif (longtime > hours) {return (Math.floor (longtime/hours) + "hours ago"); else if (longtime > minutes) {return (Math.floor (longtime/minutes) + "minutes ago"); else if (longtime > second) {return (Math.floor (Longtime/second) + "seconds Ago"); }else {return (longtime+ "error"); } document.write (Stringtodatetime ("2009-05-24 15:05:00")); </script> </HEAD> <body > </BODY> </HTML>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Online use of C # Background implementation of the method, for easy to view the paste to the following bar:
Copy Code code as follows:
public string Datestringfromnow (DateTime dt)
{
TimeSpan span = Datetime.now-dt;
if (span. Totaldays >60)
{
Return dt. ToShortDateString ();
}
Else
if (span. Totaldays >30)
{
Return
"1 months ago";
}
Else
if (span. Totaldays >14)
{
Return
"2 weeks ago";
}
Else
if (span. Totaldays >7)
{
Return
"1 weeks ago";
}
Else
if (span. Totaldays >1)
{
Return
String. Format ("{0} days ago", (int) Math.floor (span). Totaldays));
}
Else
if (span. Totalhours >1)
{
Return
String. Format ("{0} hours ago", (int) Math.floor (span). totalhours));
}
Else
if (span. Totalminutes >1)
{
Return
String. Format ("{0} minutes ago", (int) Math.floor (span). totalminutes));
}
Else
if (span. TotalSeconds >=1)
{
Return
String. Format ("{0} seconds ago", (int) Math.floor (span). TotalSeconds));
}
Else
{
Return
"1 seconds Ago";
}
}