Function Code for processing the current interval in JS

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Calculate the difference between a date string and the current date
// The input parameter format is as follows: 12:12:12
// Returns the difference string
Function GetDateDiff (datetime)
{
// Convert the 2012-12-12 12: 12: 12 string into a time object in JS,
// By default, only strings in the format of 2000/05/05 are converted into time objects.
Var dateBegin = new Date (datetime. replace (/-/g ,"/"));
Var dateEnd = new Date ();
Var dateDiff = dateEnd. getTime ()-dateBegin. getTime ();
// Calculate the number of days for the difference
Var dayDiff = Math. floor (dateDiff/(24*3600*1000 ));
Var returnstr = "";
If (dayDiff> 2) // returns the time string before the day before yesterday.
{
Return datetime;
}
Else // processing since the day before yesterday
{
Var parttime = datetime. substring (11 );
Switch (dayDiff)
{
Case 2:
Returnstr + = "the day before yesterday" + parttime;
Break;
Case 1:
Returnstr + = "yesterday" + parttime;
Break;
Default: // actions for today
Var minuteleft = Math. floor (dateDiff/(60*1000); // calculate the number of minutes of the difference
If (minuteleft> 30)
{
Returnstr + = "today" + parttime;
}
Else if (minuteleft = 0)
{
Returnstr + = Math. floor (dateDiff/1000) + "seconds ago ";
}
Else
{
Returnstr + = minuteleft + "Minutes Ago ";
}
}
Alert (returnstr );
Return returnstr;
}
}
☆Other record preparation:
//
//// Calculated hours
// Var leftSecond1 = dateDiff % (24*3600*1000) // The number of milliseconds remaining after the number of computing days
// Var hourDiff = Math. floor (leftSecond1/(3600*1000 ))
//// Calculate the number of minutes of difference
// Var leftSecond2 = leftSecond1 % (3600*1000) // The number of milliseconds remaining after the computing hours
// Var minuteDiff = Math. floor (leftSecond2/(60*1000 ))
/// Calculate the time difference in seconds
// Var leftSecond3 = leftSecond2 % (60*1000) // The number of milliseconds remaining after the number of minutes is calculated
// Var secondDiff = Math. round (leftSecond3/1000)
// Var returnstr = "";
// Function ToDate1 (datetime)
//{
/// You can convert a 20080808 string to a time object in JS.
// Return new Date (bd. replace (/^ (\ d {4}) (\ d {2}) (\ d {2}) $ /, "$1/$2/$3 "));
//}
</Script>

It's easy to use. Just reference this function.

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.