Home > Web Front-end > JS Tutorial > JAVASCRIPT realizes ordinary date conversion of how many hours ago, how many minutes ago, how many seconds_time and date

JAVASCRIPT realizes ordinary date conversion of how many hours ago, how many minutes ago, how many seconds_time and date

WBOY
Release: 2016-05-16 18:52:35
Original
1154 people have browsed it

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh it to execute
]

There is a C# background implementation available on the Internet Method, please post it below for easy viewing:) The code is 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 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";
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template