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.