14

I want to display x seconds ago based on a MySQL Timestamp.

I found the plugin called timeago

But I cannot find a way to make it display only seconds.

I'm OK with 61 seconds, or 300 seconds. Any way to convert the output to seconds with timeago, or with pure jQuery / JavaScript ?

Thanks for any help !

Edit:

Sample Case :

$time1 = "2014-02-02 23:54:04"; // plus PHP ways to format it.
//$time2 = NOW() , or date(); whatever. 

I just want to get how many seconds since $time1.

Edit 2:

Working code :

<script type="text/javascript">
$(function(){
    var d2 = new Date();
    var d1 = new Date("2014-02-02 23:54:04");
    $("a#timedif").html("Diff. Seconds : "+((d2-d1)/100).toString());
    // note that you may want to round the value
});
</script>

It outputs Diff. Seconds : NaN

jeff
  • 13,055
  • 29
  • 78
  • 136
  • see: http://stackoverflow.com/questions/14297625/work-with-a-time-span-in-javascript and http://stackoverflow.com/questions/41948/how-do-i-get-the-difference-between-two-dates-in-javascript – CodeToad Feb 02 '14 at 22:02
  • Have you managed to get the timestamp in the form of a JS object? If so, what kind of object is it? – Claudia Feb 02 '14 at 22:03
  • @impinball No, but that's no problem. I can just `echo` with PHP into the ` – jeff Feb 02 '14 at 22:04
  • please provide two sample date strings. see answer below for what to do after parsing. – CodeToad Feb 02 '14 at 22:06
  • I added the sample case. Don't have any idea how to parse into JS. Let me Google :) – jeff Feb 02 '14 at 22:10
  • why divide by 100? to get seconds from milliseconds you need one more zero. you get Nan because of string values please use new Date() – CodeToad Feb 02 '14 at 22:21

5 Answers5

14

assuming you parse the string into JavaScript date object, you can do (date2 - date1)/1000

to parse mysql format timestamp, just feed the string into new Date():

 var d2 = new Date('2038-01-19 03:14:07');
 var d1 = new Date('2038-01-19 03:10:07');

 var seconds =  (d2- d1)/1000;

fix of edit 2 in the question:

<script type="text/javascript">
$(function(){
var d2 = new Date();
var d1 = new Date("2014-02-02 23:54:04");
$("a#timedif").html("Diff. Seconds : "+((d2-d1)/1000).toString());
 });

CodeToad
  • 4,656
  • 6
  • 41
  • 53
1

If you are okay with that plugin you can modify it a little bit to work with seconds only

  var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
    seconds < 90 && substitute($l.minute, 1) ||
    minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
    minutes < 90 && substitute($l.hour, 1) ||
    hours < 24 && substitute($l.hours, Math.round(hours)) ||
    hours < 42 && substitute($l.day, 1) ||
    days < 30 && substitute($l.days, Math.round(days)) ||
    days < 45 && substitute($l.month, 1) ||
    days < 365 && substitute($l.months, Math.round(days / 30)) ||
    years < 1.5 && substitute($l.year, 1) ||
    substitute($l.years, Math.round(years));

with this part, go only with converting to seconds

see the fiddle: http://jsfiddle.net/zGXLU/1/

CBeTJlu4ok
  • 1,072
  • 4
  • 18
  • 51
0

jQuery is just a JavaScript library so JavaScript will just work within your jQuery script:

// specified date:
var oneDate = new Date("November 02, 2017 06:00:00");

// number of milliseconds since midnight Jan 1 1970 till specified date
var oneDateMiliseconds = oneDate.getTime();

// number of milliseconds since midnight Jan 1 1970 till now
var currentMiliseconds = Date.now(); 

// return time difference in milliseconds
var timePassedInMilliseconds = (currentMiliseconds-oneDateMiliseconds)/1000;
alert(timePassedInMilliseconds);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gilbert Cut
  • 113
  • 1
  • 12
0

My working with time difference for calculating each value separately

var start = new Date('Mon Jul 30 2018 19:35:35 GMT+0500');
var end = new Date('Mon Jul 30 2018 21:15:00 GMT+0500');

var hrs = end.getHours() - start.getHours();
var min = end.getMinutes() - start.getMinutes(); 
var sec = end.getSeconds() - start.getSeconds();  

var hour_carry = 0;
var minutes_carry = 0;
if(min < 0){
       min += 60;
       hour_carry += 1;
   }
hrs = hrs - hour_carry;
if(sec < 0){
       sec += 60;
       minutes_carry += 1;
   }

min = min - minutes_carry;

console.log("hrs",hrs);
console.log("min",min);
console.log("sec",sec);
console.log(hrs + "hrs " + min +"min " + sec + "sec");
0

Get difference between two timestamps in

Seconds: (get timestamp by new Date().getTime())

diff_in_time = (timestamp_1 - timestamp2);

In Days:

diff_in_days = parseInt((timestamp_1 - timestamp2) / (1000 * 3600 * 24));
Rishabh Jhalani
  • 1,049
  • 13
  • 22