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!
Java* rhinoJavaScript

Avatar of undefined
Last Comment
ltpitt

8/22/2022 - Mon
CEHJ

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?
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
CEHJ

What is your goal - to get seconds time difference?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ltpitt

ASKER
I want to get the time delta in days...
CEHJ

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

Open in new window

ASKER CERTIFIED SOLUTION
ltpitt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ltpitt

ASKER
That was the working solution for me
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.