Working with dates

58 views
Skip to first unread message

Scantron

unread,
Mar 14, 2013, 9:35:43 PM3/14/13
to heo-i...@googlegroups.com
Hello all,

I was wondering if anyone is working with dates in java script? I have some criteria to order a CBC if one has not been completed in the past 24 hours. I can get it to subtract the dates and order the tests when I hard code the dates, but when using the @@patient.lab_result.Plt.date@@, the value brings in a carriage return symbol, and I get an unterminated string constant error. anyone have a solution for this?

My js:

var dt1 = new Date(Date.parse("@@patient.lab_result.Plt.date@@"));
var one_day=1000*60*60*24;
var time_diff1 = (parseInt(dt1.getTime()-dt2.getTime())/(one_day));

if (time_diff1 > 1 || isNaN(time_diff1)){
$('#PtInr').val(1);
}

What comes in:

var dt1 = new Date(Date.parse("03/13/2013 09:46 AM
"));

In a txt file you can see a small rectangle (see attached image).

Thanks for your help!

Michael

return.jpg

Scantron

unread,
Mar 14, 2013, 10:13:51 PM3/14/13
to heo-i...@googlegroups.com
Sorry guys, I pasted incomplete information:


var dt1 = new Date();
var dt2 = new Date(Date.parse("@@patient.lab_result.Plt.date@@"));
var dt3 = new Date(Date.parse("@@patient.lab_result.INR.date@@"));
var dt4 = new Date(Date.parse("@@patient.lab_result.HGB.date@@"));
var dt5 = new Date(Date.parse("@@patient.lab_result.PartThromTime.date@@"));

var one_day=1000*60*60*24;

var time_diff1 = (parseInt(dt1.getTime()-dt2.getTime())/(one_day));
var time_diff2 = (parseInt(dt1.getTime()-dt3.getTime())/(one_day));
var time_diff3 = (parseInt(dt1.getTime()-dt4.getTime())/(one_day));
var time_diff4 = (parseInt(dt1.getTime()-dt5.getTime())/(one_day));


if (time_diff1 > 1 || isNaN(time_diff1)){
$('#INR').val(1);
}
if (time_diff2 > 1 || isNaN(time_diff2)){
$('#Plt').val(1);
}
if (time_diff3 > 1 || isNaN(time_diff3)){
$('#HGB').val(1);
}
if (time_diff4 > 1 || isNaN(time_diff4)){
$('#PTT').val(1);

Jason Murray

unread,
Mar 15, 2013, 9:38:02 AM3/15/13
to heo-i...@googlegroups.com
If you can't get that new line character to go away, here's what I would do:
Take advantage of multi-line javascript literals by putting a \ character at the end of the line, which tells js to input the next line, too:

This: (notice the \ near the end)
var dt1 = new Date("@@patient.lab_result.Plt.date@@\");

Should result in this:
var dt1 = new Date(Date.parse("03/13/2013 09:46 AM\
"));


... which should work.

________________________________
--
You received this message because you are subscribed to the Google Groups "HEO iForms" group.
To unsubscribe from this group and stop receiving emails from it, send an email to heo-iforms+...@googlegroups.com.
To post to this group, send email to heo-i...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/heo-iforms/-/oGGPmawdUfcJ.
For more options, visit https://groups.google.com/groups/opt_out.



winmail.dat

Scott Morris

unread,
Mar 15, 2013, 9:44:24 AM3/15/13
to heo-i...@googlegroups.com
Given your example, I'd suggest to put the @@ variable results in a string variable first then strip out the offending character(s).  You can do this either by stripping off the last character, keeping just the first 19 characters, or (if you're using jQuery) using the $.trim() function.

var dt1 = "@@patient.lab_result.Plt.date@@";

// take off last character
dt1
= dt1.substring(0,-1);

// keep just the first 19 characters
dt1
= dt1.substring(0,19);

// jQuery $.trim() function
dt1
= $.trim(dt1);


Jason Murray

unread,
Mar 15, 2013, 9:44:48 AM3/15/13
to heo-i...@googlegroups.com
Scott-

The way I understood what he's saying, I think the problem is that the new line character results in a js syntax error:

instead of:
var date = "1/1/2013";

the new line makes the js look like this:
var date = "1/1/2013
";

Putting the \ character at the end of the string literal allows the js to happily read the second line.

________________________________

From: heo-i...@googlegroups.com on behalf of Scott Morris
Sent: Fri 3/15/2013 9:44 AM
To: heo-i...@googlegroups.com
Subject: Re: Working with dates


--
You received this message because you are subscribed to the Google Groups "HEO iForms" group.
To unsubscribe from this group and stop receiving emails from it, send an email to heo-iforms+...@googlegroups.com.
To post to this group, send email to heo-i...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/heo-iforms/-/EtIrLg2SiroJ.
winmail.dat

Scantron

unread,
Mar 15, 2013, 5:56:40 PM3/15/13
to heo-i...@googlegroups.com
Hey guys!

Thanks for the suggestions, but nothing seems to work. I tried both Scott's suggestion and  Jason's. I'll keep playing and let you know what I get.

Michael


On Thursday, March 14, 2013 9:35:43 PM UTC-4, Scantron wrote:

Jason Murray

unread,
Mar 16, 2013, 9:57:20 AM3/16/13
to heo-i...@googlegroups.com

Can you post both your code and the generated html?

 

 

From: heo-i...@googlegroups.com [mailto:heo-i...@googlegroups.com] On Behalf Of Scantron
Sent: Friday, March 15, 2013 5:57 PM
To: heo-i...@googlegroups.com
Subject: Re: Working with dates

 

Hey guys!

--

You received this message because you are subscribed to the Google Groups "HEO iForms" group.
To unsubscribe from this group and stop receiving emails from it, send an email to heo-iforms+...@googlegroups.com.
To post to this group, send email to heo-i...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msg/heo-iforms/-/y7vPy47k_oMJ.

Michael Hudson

unread,
Mar 16, 2013, 9:10:46 PM3/16/13
to heo-i...@googlegroups.com
Jason,

Here is the code I wrote:

function DateDiff(){
var dt2 = new Date(Date.parse("@@Protime.date@@"));
var dt3 = new Date(Date.parse("@@patient.lab_result.Plt.date@@"));
var dt4 = new Date(Date.parse("@@patient.lab_result.Hgb.date@@"));
var dt5 = new Date(Date.parse("@@patient.lab_result.PTT.date@@"));
var dt1 = new Date();

var one_day=1000*60*60*24;

var time_diff1 = (parseInt(dt1.getTime()-dt2.getTime())/(one_day));
var time_diff2 = (parseInt(dt1.getTime()-dt3.getTime())/(one_day));
var time_diff3 = (parseInt(dt1.getTime()-dt4.getTime())/(one_day));
var time_diff4 = (parseInt(dt1.getTime()-dt5.getTime())/(one_day));


if (time_diff1 > 1 || isNaN(time_diff1)){
$('#PtInr').val(1);
}
if (time_diff2 > 1 || isNaN(time_diff2)){
$('#Plt').val(1);
}
if (time_diff3 > 1 || isNaN(time_diff3)){
$('#CBC').val(1);
}
if (time_diff4 > 1 || isNaN(time_diff4)){
$('#Ptth').val(1);
}
}

Here is the code from HEO:

function DateDiff(){
var dt2 = new Date(Date.parse("03/13/2013 09:46 AM
"));
var dt3 = new Date(Date.parse("03/13/2013 09:46 AM
"));
var dt4 = new Date(Date.parse("03/13/2013 09:46 AM
"));
var dt5 = new Date(Date.parse("03/13/2013 09:46 AM
"));
var dt1 = new Date();

var one_day=1000*60*60*24;

var time_diff1 = (parseInt(dt1.getTime()-dt2.getTime())/(one_day));
var time_diff2 = (parseInt(dt1.getTime()-dt3.getTime())/(one_day));
var time_diff3 = (parseInt(dt1.getTime()-dt4.getTime())/(one_day));
var time_diff4 = (parseInt(dt1.getTime()-dt5.getTime())/(one_day));


if (time_diff1 > 1 || isNaN(time_diff1)){
$('#PtInr').val(1);
}
if (time_diff2 > 1 || isNaN(time_diff2)){
$('#Plt').val(1);
}
if (time_diff3 > 1 || isNaN(time_diff3)){
$('#CBC').val(1);
}
if (time_diff4 > 1 || isNaN(time_diff4)){
$('#Ptth').val(1);
}
}

So you see the lines calling the date are getting a character inserted that are causing a line break / carriage return. If you would like the files, I will send them to you individually.

Thanks,

Michael

Jason Murray

unread,
Mar 17, 2013, 9:03:32 AM3/17/13
to heo-i...@googlegroups.com

Hi Michael-

 

What happens when you put “\” after the vgr variable? Like:

 

var dt2 = new Date(Date.parse("@@Protime.date@@\"));

 

Jason

Michael Hudson

unread,
Mar 18, 2013, 8:16:15 PM3/18/13
to heo-i...@googlegroups.com
Jason,

The same thing and no calculations. I know the calcs work if I hard code the dates, so it isn't the JS.:

--
You received this message because you are subscribed to the Google Groups "HEO iForms" group.
To unsubscribe from this group and stop receiving emails from it, send an email to heo-iforms+...@googlegroups.com.
To post to this group, send email to heo-i...@googlegroups.com.

Scott Morris

unread,
Mar 19, 2013, 9:20:19 AM3/19/13
to heo-i...@googlegroups.com
Ok, I recreated your test case to get a better idea of what the problem is and I think I have an answer for you now.  Since the @@ date variable has the line break at the end, js doesn't like what happens when you try to set your variable to it directly, so something like this won't work:

var myDate = '@@myLabDate@@';

So here's my proposal: put the @@ date variable in an element on your page (most likely hidden unless you're showing it to the user, then it can serve a double purpose).

<span id="myLabDate">@@myLabDate@@</span>

Then, in your javascript, use the DOM reference to get your date.

// If you use jQuery
var myDate = $.trim($('#myLabDate').text());

// If you don't use jQuery
var myDate = document.getElementById('myLabDate').innerHTML.replace(/\n/g,'');

Give that a shot and let me know.

Jason Murray

unread,
Mar 19, 2013, 9:25:32 AM3/19/13
to heo-i...@googlegroups.com
Michael,

Scott's solution sounds like a good way to go. Otherwise, you would need to either figure out why that line break is in your variable value or get that "\" character to be at the end of the first line, which it looks like that just won't happen.

HEO does a good job of keeping us on our toes, it seems.

Jason

________________________________
To unsubscribe from this group and stop receiving emails from it, send an email to heo-iforms+...@googlegroups.com <mailto:heo-iforms%2Bunsu...@googlegroups.com> .
winmail.dat

Michael Hudson

unread,
Mar 19, 2013, 9:30:30 AM3/19/13
to heo-i...@googlegroups.com
Amen brotha!

Michael Hudson

unread,
Mar 19, 2013, 10:26:22 AM3/19/13
to heo-i...@googlegroups.com
Scott and Jason,

Here is what finally worked (might not be eloquent, but it worked!). So let me explain what the end game is. This is for a heparin drip Iform for my current client, and they want it to automatically order the following labs if not done in the past 24 hours: PTT, PT/INR, CBC. I have hidden fields and if their value is 1 then in the VGR it orders the labs, if less than one no labs are ordered.

Thank you guys for all the help!

HTML:
<span id="protimea">@@patient.lab_result.Protime.date@@</span>
<span id="plta">@@patient.lab_result.Plt.date@@</span>
<span id="cbca">@@patient.lab_result.Hgb.date@@</span>
<span id="ptta">@@patient.lab_result.PartThromTime.date@@</span>

JS:
function DateDiff(){
var protime2 = $.trim($('#protimea').text());
var plt2 = $.trim($('#plta').text());
var hgb2 = $.trim($('#cbca').text());
var ptt2 = $.trim($('#ptta').text());

var dt1 = new Date();
var dt2 = new Date(Date.parse(protime2));
var dt3 = new Date(Date.parse(plt2));
var dt4 = new Date(Date.parse(hgb2));
var dt5 = new Date(Date.parse(ptt2));

var one_day=1000*60*60*24;

var time_diff1 = (parseInt(dt1.getTime()-dt2.getTime())/(one_day));
var time_diff2 = (parseInt(dt1.getTime()-dt3.getTime())/(one_day));
var time_diff3 = (parseInt(dt1.getTime()-dt4.getTime())/(one_day));
var time_diff4 = (parseInt(dt1.getTime()-dt5.getTime())/(one_day));

if (time_diff1 > 1 || isNaN(time_diff1)){
$('#PtInr').val(1);
}
if (time_diff2 > 1 || isNaN(time_diff2)){
$('#Plt').val(1);
}
if (time_diff3 > 1 || isNaN(time_diff3)){
$('#CBC').val(1);
}
if (time_diff4 > 1 || isNaN(time_diff4)){
$('#Ptth').val(1);
}
}

--
You received this message because you are subscribed to the Google Groups "HEO iForms" group.
To unsubscribe from this group and stop receiving emails from it, send an email to heo-iforms+...@googlegroups.com.
To post to this group, send email to heo-i...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/heo-iforms/-/HMFMKYxlPIsJ.

Jason Murray

unread,
Mar 19, 2013, 11:08:33 AM3/19/13
to heo-i...@googlegroups.com
That's awesome. I like the idea about auto-ordering labs.

________________________________

From: heo-i...@googlegroups.com on behalf of Michael Hudson
Sent: Tue 3/19/2013 10:26 AM
To: heo-i...@googlegroups.com
Subject: Re: Working with dates


To unsubscribe from this group and stop receiving emails from it, send an email to heo-iforms+...@googlegroups.com <mailto:heo-iforms%2Bunsu...@googlegroups.com> .
winmail.dat

ckittred

unread,
Mar 28, 2013, 2:45:49 PM3/28/13
to heo-i...@googlegroups.com
Response is a bit late, but I've found when importing dates from CPOE it works better to MAP the time to a hidden text field and then grab the text field value in the javascript, rather than initializing the js variable using a @@ vgr variable
Reply all
Reply to author
Forward
0 new messages