1

I've been trying to format my timestamps into the facebook/twitter style where it shows 1 second, 1 day, 1 month, 1 year and so on. However for some reason it's not returning the right values back and i'm not sure why?

Below I've made a copy of the code I'm using and made two examples of the subtracting dates.

It would be good if somebody could help me out and point out the problem and where i'm going wrong in displaying the correct value.

// Returned Result 
$("#TimeFormatted").append("Not divided by 1000: " + FormatTimeAgo(SubtractDates("1432215738000")));
$("#TimeFormatted2").append("Divided by 1000: " + FormatTimeAgo(SubtractDates2("1432215738000")));

function FormatTimeAgo(milliseconds)
{

    function numberEnding (number) {
        return (number > 1) ? 's' : '';
    }

    var temp = Math.floor(milliseconds / 1000);
    var years = Math.floor(temp / 31536000);

    // Years ago
    if (years) {
        return years + ' year' + numberEnding(years);
    }
    
    // Days ago
    var days = Math.floor((temp %= 31536000) / 86400);
    if (days) {
        return days + ' day' + numberEnding(days);
    }

    // Hours ago
    var hours = Math.floor((temp %= 86400) / 3600);
    if (hours) {
        return hours + ' hour' + numberEnding(hours);
    }

    // Minutes ago
    var minutes = Math.floor((temp %= 3600) / 60);
    if (minutes) {
        return minutes + ' minute' + numberEnding(minutes);
    }

    // Seconds ago
    var seconds = temp % 60;
    if (seconds) {
        return seconds + ' second' + numberEnding(seconds);
    }

    return 'less than a second'; //'just now' //or other string you like;
}

function SubtractDates(databaseTime)
{
    // Current time
    var date = new Date().getTime();

    // Database value
    var mysqlDate = new Date(databaseTime);
    var getMYSQLDate = mysqlDate.getTime();
    
    // Subtract dates
    var subtractDates = date - getMYSQLDate;

    return subtractDates;
}

function SubtractDates2(databaseTime)
{
    // Current time
    var date = new Date().getTime();

    // Database value
  
    var mysqlDate2 = new Date(databaseTime / 1000);
    var getMYSQLDate2 = mysqlDate2.getTime();
  
    // Subtract dates
    var subtractDates2 = date - getMYSQLDate2;

    return subtractDates2;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div id="TimeFormatted"></div>

<div id="TimeFormatted2"></div>

Thank-you in advance!

YaBCK
  • 2,949
  • 4
  • 32
  • 61
  • 4
    Just for info - you could consider `moment.js` lib – Ivan Sivak May 22 '15 at 13:31
  • @IvanSivak I'm trying to cut down on libraries, so i'm not looking to use anything like moment.js – YaBCK May 22 '15 at 13:32
  • 1
    Time manipulation can be very tricky, as @Ivan says, you should consider using `moment.js` (unless you are just practicing) – skirato May 22 '15 at 13:32
  • 1
    @skirato I'm practising and trying to improve my skills in all areas, so I'm trying to improve on Time manipulation – YaBCK May 22 '15 at 13:33

2 Answers2

2

Chris -

See this thread which appears to answer your actual question:

Stackoverflow: Convert a Unix timestamp to time in Javascript

And as an alternative to your calculations with milliseconds, consider leveraging the power of the Date object. When you subtract two date values you are left with elapsed time since 1 January 1970. Then you simply need to offset the year by -1970 and the day by -1 ... and you have the information that you want.

As Ivan Sivak noted, dates are tricky to work with ... especially whenever you're working with world timezones and seasonal time changes. That's when moment.js is helpful.

Anyway, here is some more code to experiment with in case you want to understand different approaches:

<!doctype html>
<html>
<body>
<div id="stdout"></div>

<script type="text/javascript">

var startDate = new Date( 1432215738000 );

setInterval( 
    function() {
        var t = new Date( new Date() - startDate ); 
        document.getElementById('stdout').innerHTML = (
            'year=' + ( t.getUTCFullYear() - 1970) + 
            ' day=' + (t.getUTCDate() - 1) + 
            ' hour=' + t.getUTCHours() + 
            ' minute=' + t.getUTCMinutes() + 
            ' seconds=' + t.getUTCSeconds()
        );
    }, 
    1000
);
</script>
</body>
</html>
Community
  • 1
  • 1
Yogi
  • 6,241
  • 3
  • 24
  • 30
0

I feel abit silly now, because i've answered my own question. I had forgot to do

Math.floor((Subtracted Time) / 1000);

function SubtractDates(databaseTime)
{
    // Current time
    var date = new Date().getTime();

    // Database value
    var mysqlDate = new Date(databaseTime / 1000);
    var getMYSQLDate = mysqlDate.getTime();

    // Subtract dates FIXED
    var SubtractDates = Math.floor((date - getMYSQLDate) / 1000) ;

    return SubtractDates;
}
YaBCK
  • 2,949
  • 4
  • 32
  • 61