0

I have this JavaScript code. My question is, how do I make it function?

I tried timeSince("2008"); but that doesn't show anything.

var timeSince = function(date) {
    if (typeof date !== 'object') {
        date = new Date(date);
    }

    var seconds = Math.floor((new Date() - date) / 1000);
    var intervalType;

    var interval = Math.floor(seconds / 31536000);
    if (interval >= 1) {
        intervalType = 'year';
    } else {
        interval = Math.floor(seconds / 2592000);
        if (interval >= 1) {
            intervalType = 'month';
        } else {
            interval = Math.floor(seconds / 86400);
            if (interval >= 1) {
                intervalType = 'day';
            } else {
                interval = Math.floor(seconds / 3600);
                if (interval >= 1) {
                    intervalType = "hour";
                } else {
                    interval = Math.floor(seconds / 60);
                    if (interval >= 1) {
                        intervalType = "minute";
                    } else {
                        interval = seconds;
                        intervalType = "second";
                    }
                }
            }
        }
    }

    if (interval > 1 || interval === 0) {
        intervalType += 's';
    }

    return interval + ' ' + intervalType;
};

code taken from How to format time since xxx e.g. “4 minutes ago” similar to Stack Exchange sites

Community
  • 1
  • 1
  • 1
    The function returns - "7 years" – bpavlov Jul 29 '15 at 10:53
  • This is already a function, and you are even using it correctly. Perhaps you just aren't doing anything with the value? See this [fiddle](http://jsfiddle.net/2jkff888/) – Mike Jul 29 '15 at 10:53
  • 1
    Did you read the comments in the question where you found the answer/source code or did you just copy/paste and hope it would work without you having to do anything? – NewToJS Jul 29 '15 at 10:56
  • @NewToJS Where do you see me for? I did read.. – Willy Wybert Jul 29 '15 at 10:59
  • @WillyWybert (Where) do I see you for? Sorry I don't understand. "How do I make this function" doesn't explain what you have tried and what errors you're having from your attempts. Do you understand the basics of javascript? How to call functions with arguments? We can only go by what you post and you haven't given much detail with the issue you're having other than "I tried timeSince('2008');". The function returns a value/string so you need to apply it to something. Mikes example uses the returned string to display an alert box. – NewToJS Jul 29 '15 at 11:07
  • @Mike Hey, whatever year/date I change it to, it keeps displaying "45 years". How can I fix this? – Willy Wybert Jul 29 '15 at 11:08
  • @NewToJS I'm asking a simple question here, nothing more. If you can't give me any good answers, please just don't answer. I appreciate everybody who helps, but your first comment was not even near useful and in some ways disrespectful. – Willy Wybert Jul 29 '15 at 11:13
  • @WillyWybert My first question is just a question, no disrespect intended. I'm simply asking if you did read the comments in the post you found the source code from. I don't know if you have read them or not, hence my question. Reason I believe you haven't is because you don't know how to make the answer/source code work. If you want something useful then here you go: You need to pass the correct date format into the function. Try this `alert(timeSince('Wed Jul 29 2015 12:10:49 GMT+0100 (GMT Summer Time)'));` You can get that date format from using `new Date();` http://jsfiddle.net/ksjr3f4s/ – NewToJS Jul 29 '15 at 11:19

2 Answers2

0

The code you referenced is already a function, but it doesn't output anything, it simply returns a String containing the value that you are looking for. So, you will have to use javascript to insert that String in your HTML or you can alert() it if you just want to see the value. The reason you are getting 45 years back is because you are passing an integer into the function.

The function itself uses JavaScript's built in Date() object to calculate the time. When you pass in an integer, you are actually instantiating that object with a millisecond value.

This is from W3 schools:

Using new Date(number), creates a new date object as zero time plus the number. Zero time is 01 January 1970 00:00:00 UTC. The number is specified in milliseconds:

Guess how many years have passed since 1970 (45 years!). So, if you want to know how many years have elapsed since 2000, don't use timeSince(2000), be sure to use timeSince("2000") (The quotes make all the difference).

updated Fiddle

Edited corrected typo in Fiddle

Mike
  • 1,266
  • 1
  • 11
  • 17
  • In your example you also use `alert(timeSince(1999));` not `alert(timeSince('1999'));` I think the OP changed the date without adding single/double quotes so that would return 45 years no matter which date is given. – NewToJS Jul 29 '15 at 12:04
  • I see I provided the wrong Fiddle version in my initial comment. I have updated my answer accordingly. Thank you. – Mike Jul 29 '15 at 12:11
  • Most welcome :) I could see how that could confuse the OP but a very easy mistake to make. – NewToJS Jul 29 '15 at 12:15
0

You need to pass the correct date format into the function.

Wed Jul 29 2015 12:10:49 GMT+0100 (GMT Summer Time)

alert(timeSince('Wed Jul 29 2015 12:10:49 GMT+0100 (GMT Summer Time)'));

You can get this using new Date()

You will also need to do something with the function call to see/use the returned data, I will display a few examples below.

Alert results

window.onload=function(){
var a = new Date();
alert(timeSince(a.setTime(a.getTime() - 150000)));
}
// 
var timeSince = function(date) {
if (typeof date !== 'object') {date = new Date(date);}
var seconds = Math.floor((new Date() - date) / 1000);
var intervalType;
var interval = Math.floor(seconds / 31536000);
if(interval >= 1){intervalType = 'year';
}else{interval = Math.floor(seconds / 2592000);
if (interval >= 1){intervalType = 'month';
}else{interval = Math.floor(seconds / 86400);
if(interval >= 1) {intervalType = 'day';
}else{interval = Math.floor(seconds / 3600);
if(interval >= 1) {intervalType = "hour";
}else{interval = Math.floor(seconds / 60);
if(interval >= 1) {intervalType = "minute";
}else{
interval = seconds;intervalType = "second";
}}}}}
if (interval > 1 || interval === 0){intervalType += 's';}
return interval + ' ' + intervalType;
};

Apply results to an existing element

  • getElementById()
  • innerHTML

window.onload=function(){
document.getElementById('example').innerHTML=timeSince('Wed Jul 29 2015 12:10:49 GMT+0100 (GMT Summer Time)');
}
// 
var timeSince = function(date) {
if (typeof date !== 'object') {date = new Date(date);}
var seconds = Math.floor((new Date() - date) / 1000);
var intervalType;
var interval = Math.floor(seconds / 31536000);
if(interval >= 1){intervalType = 'year';
}else{interval = Math.floor(seconds / 2592000);
if (interval >= 1){intervalType = 'month';
}else{interval = Math.floor(seconds / 86400);
if(interval >= 1) {intervalType = 'day';
}else{interval = Math.floor(seconds / 3600);
if(interval >= 1) {intervalType = "hour";
}else{interval = Math.floor(seconds / 60);
if(interval >= 1) {intervalType = "minute";
}else{
interval = seconds;intervalType = "second";
}}}}}
if (interval > 1 || interval === 0){intervalType += 's';}
return interval + ' ' + intervalType;
};
<div id="example"></div>

I hope this helps. Happy coding!

NewToJS
  • 2,762
  • 3
  • 14
  • 22