Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //problem: the output is off, a freshly generated timestamp will give like -25 seconds.
- //time = 1429378522.32 <- python time.time() stored in a database
- function genTime(time) {
- var total_seconds = +new Date()/1000 - time; //divide date by 1000 then subtract the old time
- var MINUTE = 60;
- var HOUR = MINUTE * 60;
- var DAY = HOUR * 24;
- var WEEKS = DAY * 7;
- var MONTHS = WEEKS * 4;
- var YEARS = MONTHS * 12;
- var years = Math.floor( total_seconds / YEARS);
- var days = Math.floor( total_seconds / DAY );
- var weeks = Math.floor( total_seconds / WEEKS );
- var months = Math.floor( total_seconds / MONTHS);
- var hours = Math.floor( ( total_seconds % DAY ) / HOUR );
- var minutes = Math.floor( ( total_seconds % HOUR ) / MINUTE );
- var seconds = Math.floor( total_seconds % MINUTE );
- string = {};
- if (days > 0) {
- string.day = days + " " + (days == 1 && "day" || "days" );
- }
- if (hours > 0)
- string.hours = hours + " " + (hours == 1 && "hour" || "hours" );
- if (minutes > 0) {
- string.minutes = minutes + " " + (minutes == 1 && "minute" || "minutes" );
- }
- if (weeks > 0) {
- string.weeks = weeks + " " + (weeks == 1 && "week" || "weeks" );
- }
- if (months > 0) {
- string.months = months + " " + (weeks == 1 && "week" || "weeks" );
- }
- if (years > 0) {
- string.years = + " " + (years == 1 && "year" || "years");
- }
- string.seconds = seconds + " " + (seconds == 1 && "second" || "seconds" );
- return string;
- }
- function Time(time, one) {
- var string = "";
- one = one || 1;
- if (one==0) {
- var time = genTime(time);
- if (time.years >=1) {
- return time.years;
- }
- if (time.months >= 1) {
- return time.months;
- }
- if (time.weeks <= 4) {
- return time.weeks;
- }
- if (time.days) {
- return time.days;
- }
- if (time.hours) {
- return time.hours;
- }
- if (time.minutes) {
- return time.minutes
- }
- if (time.seconds){
- return time.seconds;
- } else {
- return 0 + 'S';
- }
- }
- if (one == 1) {
- var time = genTime(time);
- if (time.years){
- string = time.years + ", ";
- }
- if (time.months) {
- string = string + time.months + ", ";
- }
- if (time.days) {
- string = string + time.days + ", ";
- }
- if (time.minutes) {
- string = string + time.minutes + ", ";
- }
- if (time.seconds) {
- string = string + time.seconds;
- }
- return string;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement