JAVASCRIPT implementation Normal date conversion how many hours ago, how many minutes ago, how many seconds _ time date

Source: Internet
Author: User
Tags datetime
<! 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";
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.