Turn a string into a date using Rhino Java

ltpitt
ltpitt used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
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?

Author

Commented:
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
Top Expert 2016

Commented:
What is your goal - to get seconds time difference?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I want to get the time delta in days...
Top Expert 2016

Commented:
var dateDelta = today.getTime() - oldDate.getTime();
alert(dateDelta/1000/60/60/24 + " day(s) between");

Open in new window

Commented:
Not working...

The final solution was that my Rhino 1.6 date object is accepting date only in the following format:
either "Month DD, YYYY" (eg, "March 2, 2017") or an array of (YYYY, Month, DD).

Author

Commented:
That was the working solution for me

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial