JS get time difference to UNIX timestamp or the timestamps date
<p>I have some elements with UNIX timestamps inside, which is not useful for clients. So i transform them into</p>

<ul>
<li><p>less then one minute ago = "just now"</p></li>
<li><p>less then one hour ago = "X minutes ago"</p></li>
<li><p>less then a day ago = "X hours ago"</p></li>
<li><p>else: "timestamp->real date".</p></li>
</ul>

<p>First of all my code works, <strong>BUT</strong> not on mobile devices such as iPhone5s. The result with these devices will be: <strong>NaN NaN NaN NaN</strong>. I'd love to know how to fix this.</p>

<p>The other thing is, my javascript looks pretty stupid, I think. But I could not figure out how to loop through these different time-conditions. Suggestions here are not necessary but grateful appreciated.</p>

<p><strong>NOTE: I cannot change the structure of my paragraphs before the page is loaded. It is set that my paragraphs will contain UNIX timestamps. Thats my starting position.</strong></p>

<p>here comes my full html with loads of &lt; p > examples, <strong>copy &amp; paste ready:</strong></p>

<pre><code>&lt;!DOCTYPE html&gt;

&lt;html&gt;

&lt;head&gt;
&lt;script&gt;
    function startTime() {

        var dates = document.getElementsByClassName('date');

        for (var i = 0; i &lt; dates.length; i++) {

            var currentdate = Date.now();

            if (dates[i].innerHTML !== '') {

                var date = new Date(dates[i].innerHTML*1000);

                if (currentdate - 60000 &lt;= date) {dates[i].innerHTML = "just now"}
                else if (currentdate - 120000 &lt;= date) {dates[i].innerHTML = "1 min ago"}
                else if (currentdate - 180000 &lt;= date) {dates[i].innerHTML = "2 mins ago"}
                else if (currentdate - 240000 &lt;= date) {dates[i].innerHTML = "3 mins ago"}
                else if (currentdate - 300000 &lt;= date) {dates[i].innerHTML = "4 mins ago"}
                else if (currentdate - 360000 &lt;= date) {dates[i].innerHTML = "5 mins ago"}
                else if (currentdate - 420000 &lt;= date) {dates[i].innerHTML = "6 mins ago"}
                else if (currentdate - 480000 &lt;= date) {dates[i].innerHTML = "7 mins ago"}
                else if (currentdate - 540000 &lt;= date) {dates[i].innerHTML = "8 mins ago"}
                else if (currentdate - 600000 &lt;= date) {dates[i].innerHTML = "9 mins ago"}
                else if (currentdate - 660000 &lt;= date) {dates[i].innerHTML = "10 mins ago"}
                else if (currentdate - 720000 &lt;= date) {dates[i].innerHTML = "11 mins ago"}
                else if (currentdate - 780000 &lt;= date) {dates[i].innerHTML = "12 mins ago"}
                else if (currentdate - 840000 &lt;= date) {dates[i].innerHTML = "13 mins ago"}
                else if (currentdate - 900000 &lt;= date) {dates[i].innerHTML = "14 mins ago"}
                else if (currentdate - 960000 &lt;= date) {dates[i].innerHTML = "15 mins ago"}
                else if (currentdate - 1020000 &lt;= date) {dates[i].innerHTML = "16 mins ago"}
                else if (currentdate - 1080000 &lt;= date) {dates[i].innerHTML = "17 mins ago"}
                else if (currentdate - 1140000 &lt;= date) {dates[i].innerHTML = "18 mins ago"}
                else if (currentdate - 1200000 &lt;= date) {dates[i].innerHTML = "19 mins ago"}
                else if (currentdate - 1260000 &lt;= date) {dates[i].innerHTML = "20 mins ago"}
                else if (currentdate - 1320000 &lt;= date) {dates[i].innerHTML = "21 mins ago"}
                else if (currentdate - 1380000 &lt;= date) {dates[i].innerHTML = "22 mins ago"}
                else if (currentdate - 1440000 &lt;= date) {dates[i].innerHTML = "23 mins ago"}
                else if (currentdate - 1500000 &lt;= date) {dates[i].innerHTML = "24 mins ago"}
                else if (currentdate - 1560000 &lt;= date) {dates[i].innerHTML = "25 mins ago"}
                else if (currentdate - 1620000 &lt;= date) {dates[i].innerHTML = "26 mins ago"}
                else if (currentdate - 1680000 &lt;= date) {dates[i].innerHTML = "27 mins ago"}
                else if (currentdate - 1740000 &lt;= date) {dates[i].innerHTML = "28 mins ago"}
                else if (currentdate - 1800000 &lt;= date) {dates[i].innerHTML = "29 mins ago"}
                else if (currentdate - 1860000 &lt;= date) {dates[i].innerHTML = "30 mins ago"}
                else if (currentdate - 1920000 &lt;= date) {dates[i].innerHTML = "31 mins ago"}
                else if (currentdate - 1980000 &lt;= date) {dates[i].innerHTML = "32 mins ago"}
                else if (currentdate - 2040000 &lt;= date) {dates[i].innerHTML = "33 mins ago"}
                else if (currentdate - 2100000 &lt;= date) {dates[i].innerHTML = "34 mins ago"}
                else if (currentdate - 2160000 &lt;= date) {dates[i].innerHTML = "35 mins ago"}
                else if (currentdate - 2220000 &lt;= date) {dates[i].innerHTML = "36 mins ago"}
                else if (currentdate - 2280000 &lt;= date) {dates[i].innerHTML = "37 mins ago"}
                else if (currentdate - 2340000 &lt;= date) {dates[i].innerHTML = "38 mins ago"}
                else if (currentdate - 2400000 &lt;= date) {dates[i].innerHTML = "39 mins ago"}
                else if (currentdate - 2460000 &lt;= date) {dates[i].innerHTML = "40 mins ago"}
                else if (currentdate - 2520000 &lt;= date) {dates[i].innerHTML = "41 mins ago"}
                else if (currentdate - 2580000 &lt;= date) {dates[i].innerHTML = "42 mins ago"}
                else if (currentdate - 2640000 &lt;= date) {dates[i].innerHTML = "43 mins ago"}
                else if (currentdate - 2700000 &lt;= date) {dates[i].innerHTML = "44 mins ago"}
                else if (currentdate - 2760000 &lt;= date) {dates[i].innerHTML = "45 mins ago"}
                else if (currentdate - 2820000 &lt;= date) {dates[i].innerHTML = "46 mins ago"}
                else if (currentdate - 2880000 &lt;= date) {dates[i].innerHTML = "47 mins ago"}
                else if (currentdate - 2940000 &lt;= date) {dates[i].innerHTML = "48 mins ago"}
                else if (currentdate - 3000000 &lt;= date) {dates[i].innerHTML = "49 mins ago"}
                else if (currentdate - 3060000 &lt;= date) {dates[i].innerHTML = "50 mins ago"}
                else if (currentdate - 3120000 &lt;= date) {dates[i].innerHTML = "51 mins ago"}
                else if (currentdate - 3180000 &lt;= date) {dates[i].innerHTML = "52 mins ago"}
                else if (currentdate - 3240000 &lt;= date) {dates[i].innerHTML = "53 mins ago"}
                else if (currentdate - 3300000 &lt;= date) {dates[i].innerHTML = "54 mins ago"}
                else if (currentdate - 3360000 &lt;= date) {dates[i].innerHTML = "55 mins ago"}
                else if (currentdate - 3420000 &lt;= date) {dates[i].innerHTML = "56 mins ago"}
                else if (currentdate - 3480000 &lt;= date) {dates[i].innerHTML = "57 mins ago"}
                else if (currentdate - 3540000 &lt;= date) {dates[i].innerHTML = "58 mins ago"}
                else if (currentdate - 3600000 &lt;= date) {dates[i].innerHTML = "59 mins ago"}
                else if (currentdate - 7320000 &lt;= date) {dates[i].innerHTML = "1 hr ago"}
                else if (currentdate - 10980000 &lt;= date) {dates[i].innerHTML = "2 hrs ago"}
                else if (currentdate - 14640000 &lt;= date) {dates[i].innerHTML = "3 hrs ago"}
                else if (currentdate - 18300000 &lt;= date) {dates[i].innerHTML = "4 hrs ago"}
                else if (currentdate - 21960000 &lt;= date) {dates[i].innerHTML = "5 hrs ago"}
                else if (currentdate - 25620000 &lt;= date) {dates[i].innerHTML = "6 hrs ago"}
                else if (currentdate - 29280000 &lt;= date) {dates[i].innerHTML = "7 hrs ago"}
                else if (currentdate - 32940000 &lt;= date) {dates[i].innerHTML = "8 hrs ago"}
                else if (currentdate - 36600000 &lt;= date) {dates[i].innerHTML = "9 hrs ago"}
                else if (currentdate - 40260000 &lt;= date) {dates[i].innerHTML = "10 hrs ago"}
                else if (currentdate - 43920000 &lt;= date) {dates[i].innerHTML = "11 hrs ago"}
                else if (currentdate - 47580000 &lt;= date) {dates[i].innerHTML = "12 hrs ago"}
                else if (currentdate - 51240000 &lt;= date) {dates[i].innerHTML = "13 hrs ago"}
                else if (currentdate - 54900000 &lt;= date) {dates[i].innerHTML = "14 hrs ago"}
                else if (currentdate - 58560000 &lt;= date) {dates[i].innerHTML = "15 hrs ago"}
                else if (currentdate - 62220000 &lt;= date) {dates[i].innerHTML = "16 hrs ago"}
                else if (currentdate - 65880000 &lt;= date) {dates[i].innerHTML = "17 hrs ago"}
                else if (currentdate - 69540000 &lt;= date) {dates[i].innerHTML = "18 hrs ago"}
                else if (currentdate - 73200000 &lt;= date) {dates[i].innerHTML = "19 hrs ago"}
                else if (currentdate - 76860000 &lt;= date) {dates[i].innerHTML = "20 hrs ago"}
                else if (currentdate - 80520000 &lt;= date) {dates[i].innerHTML = "21 hrs ago"}
                else if (currentdate - 84180000 &lt;= date) {dates[i].innerHTML = "22 hrs ago"}
                else if (currentdate - 87840000 &lt;= date) {dates[i].innerHTML = "23 hrs ago"}
                else {

                    var year = date.getFullYear();
                    var month = date.getMonth() + 1;
                    var day = date.getDate();
                    var formattedTime = day + "." + month + "." + year;

                    dates[i].innerHTML = formattedTime;

                }   
            }
        }
    }
&lt;/script&gt;

&lt;/head&gt;


&lt;body onload="startTime();"&gt;

&lt;p class="date"&gt;1483015785&lt;/p&gt;
&lt;p class="date"&gt;1483015786&lt;/p&gt;
&lt;p class="date"&gt;1483015794&lt;/p&gt;
&lt;p class="date"&gt;1483015984&lt;/p&gt;
&lt;p class="date"&gt;1483019784&lt;/p&gt;
&lt;p class="date"&gt;1483095784&lt;/p&gt;
&lt;p class="date"&gt;1489915784&lt;/p&gt;
&lt;p class="date"&gt;1483015927&lt;/p&gt;
&lt;p class="date"&gt;1483015918&lt;/p&gt;
&lt;p class="date"&gt;1483015917&lt;/p&gt;
&lt;p class="date"&gt;1483015728&lt;/p&gt;
&lt;p class="date"&gt;1483013928&lt;/p&gt;
&lt;p class="date"&gt;1483012928&lt;/p&gt;
&lt;p class="date"&gt;1483011928&lt;/p&gt;
&lt;p class="date"&gt;1483010928&lt;/p&gt;
&lt;p class="date"&gt;1483011428&lt;/p&gt;
&lt;p class="date"&gt;1483031228&lt;/p&gt;
&lt;p class="date"&gt;712619600&lt;/p&gt;
&lt;p class="date"&gt;716165789&lt;/p&gt;
&lt;p class="date"&gt;451616960&lt;/p&gt;
&lt;p class="date"&gt;567845845&lt;/p&gt;
&lt;p class="date"&gt;767856785&lt;/p&gt;
&lt;p class="date"&gt;867985764&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<h1>UPDATE:</h1>

<p>I did a little bit more research, as it turns out the <strong>problem occurs because mobile Safari automatically turns certain numbers into links!</strong></p>

<p>example:</p>

<pre><code>&lt;p&gt;1484234234234&lt;/p&gt;
automatically becomes
&lt;p&gt;&lt;a href="tel:1484234234234"&gt;1484234234234&lt;/a&gt;&lt;/p&gt;
</code></pre>

<p>Thats at least why my code isn't working. Strange is, that not all numbers will be converted, as you can find out with this code:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;script&gt;
    function startTime() {
        var dates = document.getElementsByClassName('date');    
        for (var i = 0; i &lt; dates.length; i++) {
            if (dates[i].innerHTML !== '') {
                var date = dates[i].innerHTML;
                alert(date);
    }}}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="startTime();"&gt;

&lt;!--first two will be converted--&gt;
    &lt;p class="date"&gt;1483015785&lt;/p&gt;
    &lt;p class="date"&gt;1483015786&lt;/p&gt;
&lt;!--these two won't--&gt;
    &lt;p class="date"&gt;712619600&lt;/p&gt;
    &lt;p class="date"&gt;716165789&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>for now I couldn't find a solution for this problem. changing the &lt; p > to something else  ( &lt; date > f.e.) doesn't help.</p>