web stats
Date issues in javascript - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 02-28-2018, 03:28 AM
stormcel stormcel is offline
OBX.3 Kenobi
 
Join Date: Mar 2010
Location: Phoenix
Posts: 173
stormcel is on a distinguished road
Default Date issues in javascript

So I wrote code to compare date ranges.
It works in my javascript tester, but it produces 'invalid date' messages in mirth.
this is the function that I put in the code template and sample calls that work in a simulator, but not in a mirth channel:
Code:
function thisToDate(indate,dayOffset)	{
var indateYear=indate.substr(0,4);
var indateMonth=indate.substr(4,2);
var indateDay=indate.substr(6,2);
var indateDone=indateYear + '.' + indateMonth + '.' +indateDay;
var doneDate = new Date(indateDone);
doneDate.setDate(doneDate.getDate() + dayOffset);

return doneDate;
}

var testdatea = '20180215';
var testdated = '20180218';
var priorAdmit = thisToDate(testdatea,-3);
var testDischarge = thisToDate(testdated,0);
alert(priorAdmit + ' , ' + testDischarge)
As you can see this function takes a date input as YYYYMMdd and converts it to an actual date object to compare dates.
And, as I said, it works brilliantly in a javascript tester, but not in MIrth.
The logger in mirth is saying that the result of the function are invalid dates.
Please, please can someone point me in the right direction.
Reply With Quote
  #2  
Old 02-28-2018, 03:38 AM
siddharth siddharth is offline
Mirth Guru
 
Join Date: Feb 2013
Posts: 841
siddharth is on a distinguished road
Default

alert() won't work in mirth but in browser.

If your task is to compare 2 different dates, I don't think you should be using string replacement techniques.

Take a dive in joda API.
__________________
HL7v2.7 Certified Control Specialist!
Reply With Quote
  #3  
Old 02-28-2018, 03:52 AM
stormcel stormcel is offline
OBX.3 Kenobi
 
Join Date: Mar 2010
Location: Phoenix
Posts: 173
stormcel is on a distinguished road
Default

The alert was only when testing on the javascript testing environment.
In mirth I used the logger message.
That is how I know that mirth is reporting these as invalid dates.

all I need to accomplish is to make certain that input dates fall with the range of the visit minus 3 days.
That explains the example.
so....
Code:
var testProcedure = thisToDate(procedureDatetmp,0);

if (testProcedure < priorAdmit || testProcedure > testDischarge)	{
// here is where the logic goes for an out of date range procedure
}
So these dates have to be calculated correctly.
consider that 20180201 and 20180131 are only one day apart, but a difference of 70.
I'm flagging procedures that occur prior to 3 days of the visit or after discharge.

In the javascript online tester all of this worked great, whether I sent the results to an alert or to the console.

Obviously in Mirth I didn't use the alert statement.
I was just showing the code that worked in the tester.

So, Again,
what is wrong with this code, and how can I correct it to work in Mirth?
Please and thank you
Reply With Quote
  #4  
Old 02-28-2018, 04:32 AM
siddharth siddharth is offline
Mirth Guru
 
Join Date: Feb 2013
Posts: 841
siddharth is on a distinguished road
Default

You could do this instead. Simple and less complicated. The duration object of joda, actually just calculates difference between the first and second parameter. (second minus first i think)

Code:
apptTimeEST='20180224'
now=new org.joda.time.DateTime();
doAppt = new org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(apptTimeEST);
duration= new org.joda.time.Duration(doAppt,now);
diffDays=duration.getStandardDays();
logger.info(diffDays);
logger.info((diffDays > 0 && diffDays < 3) ? true : false);
__________________
HL7v2.7 Certified Control Specialist!

Last edited by siddharth; 02-28-2018 at 04:33 AM. Reason: foo
Reply With Quote
  #5  
Old 02-28-2018, 06:50 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,176
agermano is on a distinguished road
Default

This is what is wrong with the code when running in a Rhino environment.

Code:
js> new Date('2018.02.15')
Invalid Date
This is how you can fix it if you still want to work with Javascript dates. The months are zero-based.

Code:
js> new Date('2018', '02' - 1, '15')
Thu Feb 15 2018 00:00:00 GMT-0000 (GMT)
Assuming you are running on Java 8, since Mirth now requires it, I'd recommend use the java.time classes. These are based on joda time and are now part of the java platform. The joda time website recommends migrating to java.time. https://docs.oracle.com/javase/8/doc...e-summary.html

If you replace your function with this, I think the rest of your code will work. It also makes the dayOffset optional, so you don't need to put the 0 in the function call.

Code:
function thisToDate(indate,dayOffset) {
	if (typeof dayOffset == 'undefined')  {
		dayOffset = 0;
	}
	return java.time.LocalDate.parse(indate, java.time.format.DateTimeFormatter.ofPattern('yyyyMMdd')).plusDays(dayOffset);
}
Reply With Quote
  #6  
Old 03-01-2018, 01:05 AM
stormcel stormcel is offline
OBX.3 Kenobi
 
Join Date: Mar 2010
Location: Phoenix
Posts: 173
stormcel is on a distinguished road
Default

agermano, thank you so much for your response.
I'm trying to get your first syntax to work in the function, but I keep getting an invalid javascript warning.
I know it's my syntax, can you see what I'm doing wrong.
I'm trying to get the format '2018', '02' - 1, '15' that you used.

Code:
function thisToDate(indate,dayOffset)	{
var indateYear=indate.substr(0,4);
var indateMonth=indate.substr(4,2);
var indateDay=indate.substr(6,2);
var indateDone="'" + indateYear + "', '" + indateMonth + "' " + "-1, " + "'" + indateDay + "'";
var doneDate = new Date(indateDone);
doneDate.setDate(doneDate.getDate() + dayOffset);

return doneDate;
}

thisTodDate('20180201',0);
Thank you for any help you can provide.
Reply With Quote
  #7  
Old 03-01-2018, 04:51 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,176
agermano is on a distinguished road
Default

I'd really recommend using the java tools for creating the date objects. They are far more robust.

https://developer.mozilla.org/en-US/...l_Objects/Date

The constructor that takes a dateString is really limited it what it will recognize. In the javascript example I showed you, it was using the last constructor that takes year, month, and day as separate Integer parameters, not a single string. Even though you're passing in strings, javascript will recognize what you're trying to do and coerce them to Integers.
Reply With Quote
  #8  
Old 03-01-2018, 05:13 AM
aTom aTom is offline
OBX.3 Kenobi
 
Join Date: Feb 2016
Location: Lake Geneva Area
Posts: 115
aTom is on a distinguished road
Default

Hi,

There's an easier way to convert your date string to a Date object: DateUtil.getDate(pattern, date).
__________________
Tom
Reply With Quote
  #9  
Old 03-01-2018, 05:37 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,176
agermano is on a distinguished road
Default

Quote:
Originally Posted by aTom View Post
Hi,

There's an easier way to convert your date string to a Date object: DateUtil.getDate(pattern, date).
True, but he also wanted to be able to pass a dayOffset to his function. DateUtil.getDate(pattern, date) returns a java.util.Date, and adding or subtracting days requires additional java code using Calendars unless you convert it to a joda-time or java.time object anyway.
Reply With Quote
  #10  
Old 03-01-2018, 05:55 AM
aTom aTom is offline
OBX.3 Kenobi
 
Join Date: Feb 2016
Location: Lake Geneva Area
Posts: 115
aTom is on a distinguished road
Default

I would not use an external library for this, I'd keep it as simple as possible :

Code:
// I'd add some datatype checks first ...

var dateObj = DateUtil.getDate('yyyyMMdd', indate);
dateObj.setTime(date.getTime()+(dayOffset*24*60*60*1000)).
return DateUtil.formatDate('yyyyMMdd', dateObj);
__________________
Tom
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 04:13 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation