Link to home
Start Free TrialLog in
Avatar of ltpitt
ltpitt

asked on

Turn a string into a date using Rhino Java

Hi all,

Using Javascript I get a NaN:

var oldDate= '20160306 stuff stuff stuff';
var regEx = /(\d{4}\d{2}\d{2}).*/;
var match = regEx.exec(oldDate);
oldDate = match[1];

function yyyymmdd(dateIn) {
   var yyyy = dateIn.getFullYear();
   var mm = dateIn.getMonth()+1;
   var dd  = dateIn.getDate();
   return String(10000*yyyy + 100*mm + dd);
}

var today = new Date();

var dateDelta = (yyyymmdd(today) - oldDate);


Since this is working in JSfiddle but failing in Rhino I think I'd need to use Rhino Java methods to turn the oldDate string into a date object so I can get the timedelta between the oldDate and now.

How can I obtain this result?

Thanks!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

var regEx = /(\d{4})(\d{2})(\d{2}).*/;
var match = regEx.exec(oldDate);
oldDate = new Date(match[1],match[2],match[3]);

Open in new window


Isn't the above more like what you need?
Avatar of ltpitt
ltpitt

ASKER

var int_pass_rotation = '20170302 stuff stuff stuff';
var regEx = /(\d{4}\d{2}\d{2}).*/;
var match = regEx.exec(int_pass_rotation);
var oldDate = new Date(match[1],match[2],match[3]).getTime() / 1000;

oldDate;


I've tried this and the result is NaN
What is your goal - to get seconds time difference?
Avatar of ltpitt

ASKER

I want to get the time delta in days...
var dateDelta = today.getTime() - oldDate.getTime();
alert(dateDelta/1000/60/60/24 + " day(s) between");

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ltpitt
ltpitt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ltpitt

ASKER

That was the working solution for me