How many hours before normal date conversion, how many minutes ago, and how many seconds _ time date

Source: Internet
Author: User
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 ";
}
}

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.