JS in the processing of the current time interval of the function code _ time and date

Source: Internet
Author: User
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.

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.