1

I need to calculate the time difference between two timestamps in the following format:

yyyyMMddHHmmss

for example I have

var lastExecution = 20150116165100; //--- 16:51:00 16/01/2015
var currentTime =   20150116170120; //--- 17:01:20 16/01/2015

In this particular case, the elapsed time is 00:10:20.

How can I calculate it in js? Do I have to convert it in some other format before I can proceed?

What I want to achieve is to be able to set a minimum elapsed time in a variable, if the time elapsed between the lastExecution and now is greater than the minumum elapsed time I want to launch a particular function:

minTime = 00000000001000; //--- 10 minutes
var lastExecution = getLastExecutionTimestamp();
var currTime = getCurrentTimestamp();

if((currTime - lastExecution) > minTime){ //--- I need help here to calculate the elapsed time
    doSomething();
}

getLastExecutionTimestamp() and getCurrentTimestamp() return a numeric timestamp in yyyyMMddHHmmss format, for example 20150116165100

Thank you

BeNdErR
  • 17,471
  • 21
  • 72
  • 103
  • 2
    What do those functions do? Have you checked the results before comparing? What does "totally wrong" look like? Why are you trying to comapre against a string? – Teemu Jan 16 '15 at 15:05
  • @Teemu nothing in particular. They return a timestamp in `yyyyMMddHHmmss` format. I'm going to update the question – BeNdErR Jan 16 '15 at 15:07
  • @Teemu you are right, I didn't mean to use strings but numbers instead. Thank you, question updated again – BeNdErR Jan 16 '15 at 15:11
  • `00000000001000` in JS is not ten minutes, the time unit is a millisecond. Also drop off the leading zeros, that number might be interpreted as an octal number. – Teemu Jan 16 '15 at 15:13
  • Make the functions to return a Date object or an actual time instead of a timestamp. Timestamps are a combination of 10, 12, 60 and month length based numbers, you can't expect calculating with them to result a correct 10 based value. – Teemu Jan 16 '15 at 15:19

3 Answers3

0

I don't think there's really a simple and elegant way to do this. I would yoink the date values out of both strings, convert them to seconds or whatever floats your boat, then subtract and measure it against minTime, like this:

var lastYearSeconds = parseInt(lastExecution.substr(0,4))*31536000; //this is in seconds
var lastMonthSeconds = parseInt(lastExecution.substr(4,2))*2592000;

Then just do the same with all the other values, add them, then subtract from the values you parse from currTime().

Wait, forgot about the Date() class in javascript. Yay for builtins!

Basically, parse ONLY the int values from the date Strings, then feed them to the constructor like so. Then, you can subtract the two date values with dateObject.getTime() - otherDateObject.getTime()

0

Try this, this uses the format what you want:

function strToDate(str) {
  var year = parseInt(str.substring(0,4));
  var month = parseInt(str.substring(4,6));
  var day = parseInt(str.substring(6,8));
  var hour = parseInt(str.substring(8,10));
  var minute = parseInt(str.substring(10,12));
  var second = parseInt(str.substring(12,14));

  var date = new Date(year, month, day , hour, minute, second, 0);
  return date.getTime();
}

function timeStampToDate(timestamp) {
  var d = new Date(timestamp);

    var diffStr = ""+(d.getFullYear()-1970)+":"+d.getMonth()+":"+(d.getDate()-1)+":"+(d.getHours()-1)+":"+d.getMinutes()+":"+d.getSeconds();
    return diffStr;
}

var lastExecution = "20150116165100"; //--- 16:51:00 16/01/2015
var currentTime =   "20150116170120"; //--- 17:01:20 16/01/2015
var lastExecutionDate = strToDate(lastExecution);
var currentTimeDate = strToDate(currentTime);

var difference = (currentTimeDate - lastExecutionDate);
var diffInTime = timeStampToDate(difference);
alert("Time difference: "+diffInTime);

Link: http://jsfiddle.net/nxjk1L8s/

szpetip
  • 333
  • 6
  • 12
0

By using Java 1.8 the following script worked for me. Try this...

LocalDateTime now3 = LocalDateTime.now();       
System.out.println(now3);
Thread.sleep(70000);
LocalDateTime now4 = LocalDateTime.now(); 
System.out.println(now4);   
Duration duration2 = Duration.between(now3, now4);
System.out.println("duration is "+duration2);

o/p:

2023-04-27T19:14:11.968
2023-04-27T19:15:21.974
duration is PT1M10.006S
SambaM
  • 1
  • 2