JS implementation a few minutes ago, a few hours ago, a few days ago, and a few minutes later, a few hours later, a few days before and after

Source: Internet
Author: User






JS implementation a few minutes ago, a few hours ago, a few days ago, and a few minutes later, a few hours later, a few days before and after


/ * *
*Compare the incoming timestamp with the current time, and calculate the time before, hours, days, minutes, hours, and days
*UNIX time stamp to be calculated, reserved to seconds
* * /
function getDateTimeFormat(unixtime) {
var currTime = Date.parse(new Date());;
var time = ((parseInt(currTime)/1000) - parseInt(unixtime)) ;
if(time > 0 ){
//Less than one minute
if (time < 60) {
Return "wait";
}
//Seconds to minutes
var minuies = time / 60;
if (minuies < 60) {
Return math. Floor (minutes) + "in minutes";
}
//Second to hour
var hours = time / 3600;
if (hours < 24) {
Return math. Floor (hours) + "after hours";
}
//Seconds to days
var days = time / 3600 / 24;
if (days < 30) {
Return math. Floor (days) + day after;
}
/ / sec
var months = time / 3600 / 24 / 30;
if (months < 12) {
Return math. Floor (months) + "after month";
}
/ / second year
var years = time / 3600 / 24 / 30 / 12;
Return math. Floor (years) + "after year";
}else{
time = Math.abs(time);
//Less than one minute
if (time < 60) {
Return "just";
}
//Seconds to minutes
var minuies = time / 60;
if (minuies < 60) {
Return math. Floor (minutes) + "minutes ago";
}
//Second to hour
var hours = time / 3600;
if (hours < 24) {
Return math. Floor (hours) + "hours ago";
}
//Seconds to days
var days = time / 3600 / 24;
if (days < 30) {
Return math. Floor (days) + "days ago";
}
/ / sec
var months = time / 3600 / 24 / 30;
if (months < 12) {
Return math. Floor (months) + "before the month";
}
/ / second year
var years = time / 3600 / 24 / 30 / 12;
Return math. Floor (years) + "years ago";
}
} 





JS implementation a few minutes ago, a few hours ago, a few days ago, and a few minutes later, a few hours later, a few days before and after


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.