It seems to be very popular recently. I wrote a js function to implement it.
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <meta name = "Generator "CONTENT =" EditPlus "> <meta name =" Author "CONTENT =" "> <meta name =" Keywords "CONTENT =" "> <meta name =" Description "CONTENT = ""> <script language = "javascript"> function stringToDateTime (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 month ago ";} else if (longtime> days * 7) {return ("1 week ago");} else if (longtime> days) {return (Math. floor (longtime/days) + "days Ago");} else if (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 ("15:05:00"); script </HEAD> <BODY> </HTML>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The C # background implementation method is useful on the Internet. For ease of viewing, paste it below :)
The Code is as follows:
Public string DateStringFromNow (DateTime dt)
{
TimeSpan span = DateTime. Now-dt;
If (span. TotalDays> 60)
{
Return dt. tow.datestring ();
}
Else
If (span. TotalDays> 30)
{
Return
"1 month ago ";
}
Else
If (span. TotalDays> 14)
{
Return
"2 weeks ago ";
}
Else
If (span. TotalDays> 7)
{
Return
"1 week 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 second ago ";
}
}