web stats
Date issues in javascript - Page 2 - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #11  
Old 03-01-2018, 06:36 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,176
agermano is on a distinguished road
Default

That actually does use an external library You're using com.mirth.connect.server.userutil.DateUtil. The code I posted uses only Java SE 8.
Reply With Quote
  #12  
Old 03-01-2018, 08:00 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

Yeah ok, my intention was just to point out that getting a date out of a string is extremely easy in Mirth (and only in Mirth) using DateUtil package.
__________________
Tom
Reply With Quote
  #13  
Old 03-01-2018, 02:13 PM
stormcel stormcel is offline
OBX.3 Kenobi
 
Join Date: Mar 2010
Location: Phoenix
Posts: 173
stormcel is on a distinguished road
Default

I love you guys, and I appreciate your help!
But none of this is working!
NOTE: I HAVE NOT TRIED YOUR SUGGESTIONS IN MIRTH YET.
I have been using a javascript tester online.
My code worked perfectly in the tester, but failed in MIrth.
All of your suggestions are failing on the javascript tester and returning an invalid javascript method.

Is it not possible to have a script that will validate as well formed javascript AND function correctly in Mirth?
Are these failed tests of your contributed code going to work in Mirth, but be untestable in any development environment?

I am not complaining at all, I'm just trying to acclimatize myself to the actual environment that I will be expected to negotiate.

That being said, i tested the very first solution that agermano suggested. i.e.
new Date('2018', '02' - 1, '15');
and it worked in the online javascript tester!
So I tried to adjust my code accordingly, but failed to pass variable in the proper form, i.e. 'yyyy', 'MM' -/+?, 'dd'

I tried escaping the single quotes with backslashes, I tried double-quoting the single quotes, I tried not using the quotes, but failed each time.

Once again, thank you so much for your help, and please help me steer through this.

Last edited by stormcel; 03-01-2018 at 02:22 PM.
Reply With Quote
  #14  
Old 03-01-2018, 09:32 PM
aTom aTom is offline
OBX.3 Kenobi
 
Join Date: Feb 2016
Location: Lake Geneva Area
Posts: 115
aTom is on a distinguished road
Default

If you want a 100% JS function then do not consider agermano's or my suggestions - they will only work in a Rhino resp Mirth environment.

I tested the script from your 1st post in an online JS tester and it didn't work at all for me. I just replaced '.' by '/' and then it was OK.

Code:
var indateDone=indateYear + '/' + indateMonth + '/' +indateDay;
__________________
Tom
Reply With Quote
  #15  
Old 03-02-2018, 08:07 PM
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 stormcel View Post
That being said, i tested the very first solution that agermano suggested. i.e.
new Date('2018', '02' - 1, '15');
and it worked in the online javascript tester!
So I tried to adjust my code accordingly, but failed to pass variable in the proper form, i.e. 'yyyy', 'MM' -/+?, 'dd'
Your issue with trying that solution is that you're trying to pass 1 parameter instead of 3. Something like this should work in your online tester:

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

return doneDate;
}
(I'd still suggest grabbing the part from my other solution that makes dayOffset default to 0 if you call your function like thisToDate('20180302').)

As aTom mentioned, Mirth uses Mozilla Rhino as its javascript engine. Pure javascript like you are likely to find in an online tester doesn't have access to java classes, which is a huge asset in Mirth. You actually can test in Rhino outside of Mirth using scripts or an interactive shell.
Reply With Quote
  #16  
Old 03-06-2018, 10:44 PM
stormcel stormcel is offline
OBX.3 Kenobi
 
Join Date: Mar 2010
Location: Phoenix
Posts: 173
stormcel is on a distinguished road
Default

Quote:
Originally Posted by agermano View Post

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);
}
HELP!!
I couldn't get any of your suggestions to work in my online tester AND in mirth.
So I gave up and decided to just use your Rhino suggestions.
I tried the above and got this error.
Code:
Transformer error
ERROR MESSAGE: Error evaluating transformer
com.mirth.connect.server.MirthJavascriptTransformerException: 
CHANNEL:	Date-Range_tester
CONNECTOR:	sourceConnector
SCRIPT SOURCE:	TRANSFORMER
SOURCE CODE:	
12: }
13: function thisToDate(indate,dayOffset) {
14: 	if (typeof dayOffset == 'undefined')  {
15: 		dayOffset = 0;
16: 	}
17: 	return java.time.LocalDate.parse(indate, java.time.format.DateTimeFormatter.ofPattern('yyyyMMdd')).plusDays(dayOffset);
18: }
19: function validate(mapping, defaultValue, replacement) {
20:     var result = mapping;
21:     if ((result == undefined) || (result.toString().length == 0)) {
LINE NUMBER:	17
DETAILS:	Cannot convert 20180224 to java.lang.CharSequence
	at d7505578-de2d-46f6-987f-ef96a52f19b7:17 (thisToDate)
	at d7505578-de2d-46f6-987f-ef96a52f19b7:1573 (doTransform)
	at d7505578-de2d-46f6-987f-ef96a52f19b7:1735 (doScript)
	at d7505578-de2d-46f6-987f-ef96a52f19b7:1737
	at com.mirth.connect.server.transformers.JavaScriptFilterTransformer$FilterTransformerTask.doCall(JavaScriptFilterTransformer.java:154)
	at com.mirth.connect.server.transformers.JavaScriptFilterTransformer$FilterTransformerTask.doCall(JavaScriptFilterTransformer.java:119)
	at com.mirth.connect.server.util.javascript.JavaScriptTask.call(JavaScriptTask.java:113)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
I can't believe that I can't get this thing working!!
Please Please help me understand this!
And thanks again!
Reply With Quote
  #17  
Old 03-07-2018, 04:38 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

Quote:
DETAILS: Cannot convert 20180224 to java.lang.CharSequence
Exception says it all : indate is a (JS) number while LocalDate.parse waits for a (Java) CharSequence, which is equivalent to a String (see javadoc for String class).

Rhino automatically converts a JS number to a numeric Java type (a long I suppose) and a JS string into a Java String. But it won't cast a JS number into a Java String, you have to do it manually.
__________________
Tom
Reply With Quote
  #18  
Old 03-07-2018, 10:19 AM
stormcel stormcel is offline
OBX.3 Kenobi
 
Join Date: Mar 2010
Location: Phoenix
Posts: 173
stormcel is on a distinguished road
Default

So I changed:
Quote:
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);
}
To This:
Code:
function thisToDate(indate,dayOffset) {
	indate=indate.toString();
if (typeof dayOffset == 'undefined')  {
		dayOffset = 0;
	}
	return java.time.LocalDate.parse(indate, java.time.format.DateTimeFormatter.ofPattern('yyyyMMdd')).plusDays(dayOffset);
}
Seems to be working!
Thanks All.
If there is an issue, I'll be back.
And Thanks again
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 02:57 AM.


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