Copy Code code as follows:
<script type= "Text/javascript" >
Calculates the difference between a date string and the current date
The input parameter form is as follows: 2012-12-12 12:12:12
String that returns the value of the difference
function Getdatediff (datetime)
{
The 2012-12-12 12:12:12 string can be converted to a period object in JS.
Because by default only strings that are in the form of 2000/05/05 are converted to time objects
var datebegin = new Date (Datetime.replace (/-/g, "/"));
var dateend = new Date ();
var DateDiff = Dateend.gettime ()-datebegin.gettime ();
Calculate the difference between days
var Daydiff = Math.floor (DateDiff/(24 * 3600 * 1000));
var returnstr = "";
if (Daydiff > 2)//The time string is returned directly before the day before yesterday
{
return datetime;
}
else//processing since the day before yesterday
{
var parttime = datetime.substring (11);
Switch (Daydiff)
{
Case 2:
Returnstr = "day before yesterday" + parttime;
Break
Case 1:
RETURNSTR = "Yesterday" + parttime;
Break
Default://For today's operation
var minuteleft = Math.floor (DateDiff/(60 * 1000)); Calculate the number of minutes between differences
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 records:
//
Calculate the number of hours
var leftSecond1 = dateDiff% (24 * 3600 * 1000)//number of milliseconds remaining after counting days
var Hourdiff = Math.floor (LeftSecond1/(3600 * 1000))
Calculate the difference of minutes
var leftSecond2 = leftSecond1% (3600 * 1000)//number of milliseconds remaining after calculating hours
var Minutediff = Math.floor (LeftSecond2/(60 * 1000))
Count the difference in seconds
var leftSecond3 = leftSecond2% (60 * 1000)//number of milliseconds remaining after counting minutes
var Seconddiff = Math.Round (leftsecond3/1000)
var returnstr = "";
function ToDate1 (datetime)
//{
You can convert a 20080808 string into a period object in JS
return new Date (Bd.replace (/^ (\d{4}) (\d{2}) (\d{2}) $/, "$1/$2/$3"));
//}
</script>
It's easy to use, just refer to this function directly.