We help IT Professionals succeed at work.

Turn a string into a date using Rhino Java

ltpitt
ltpitt asked
on
329 Views
Last Modified: 2017-03-19
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

CERTIFIED EXPERT
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
What is your goal - to get seconds time difference?

Author

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

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

Open in new window

Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
That was the working solution for me

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions