Home
Analytics
How to set date element value from variable from script?
akk2
<p>Hi, so in OnCreate scripts, I'm doing computations and assign a date to a variable. Later on, in a data element OnCreate script, I'm trying to assign the value of the date variable to the current element. </p>
<p> </p>
<p>So if I have in the OnCreate script of the data element:</p>
<p><em>this.setDisplayValue(vars["out_start"]); </em> I get as output: <em>org.mozilla.javascript.NativeDate@87dbe2a</em></p>
<p> </p>
<p>If I try to set the data element type as date and use the following:</p>
<p><em>this.setDisplayValue(vars["out_start"].getTime()); </em> I get as output: <em>1383326333000</em></p>
<p> </p>
<p>And while trying to find how, I did a mistake and entered:</p>
<p><em>this.setDisplayValue(vars["out_start"]getTime())</em> and I got as output: <em>1 nov. 2013 14:56</em> </p>
<p>which is exactly what I'm trying to get! But I'm also got an error message at the bottom of the page.. <em>UnhandledScriptError ... Missing ')' after an argument list .. </em></p>
<p> </p>
<p>So I'm quite puzzled as to why the invalid line gives the the dates exactly like I want! :blink: Although with the exception it displays at the end of the report, that sadly isn't viable... So it appears like I'm really close, but nothing I've tried works 100% so far..</p>
<p> </p>
<p>Thanks!</p>
Find more posts tagged with
Comments
JFreeman
<p>Instead of using the native javascript date you should try using the BIRT DateTime instead.</p>
<p> </p>
<p>Take a look at this documentation on BIRT DateTime: <a data-ipb='nomediaparse' href='
http://help.eclipse.org/indigo/index.jsp?topic=/org.eclipse.birt.doc/birt/ScriptingReference.25.3.html'>http://help.eclipse.org/indigo/index.jsp?topic=/org.eclipse.birt.doc/birt/ScriptingReference.25.3.html</a></p>
;
akk2
<p>Actually it's a bit weird because I never explicitly use javascript Dates... I mostly just assign a row's date to the Birt report variables, and this seems to convert the date to javascript dates... Although sometimes I don't do it directly;</p>
<p> </p>
<p>vars["x"] = row["TIME"];</p>
<p> -- vs --</p>
<p>var time = row["TIME"];<br>
if ( ... )<br>
vars["z"] = time;</p>
<p> </p>
<p>Is that an issue? The issue with BirtDateTime is that it seems that there's no constructor or method to take a javascript date or time in ms to initialize the object... So the setYear, setMonth, setHour, setSecond, etc., all have to be used to initiate a BirtDateTime? Anyway, I guess that's what I could do in the OnCreate of the data elements I want to display, just seems like there should be an easier way of setting a 'Birt' date from javascript rather than creating a new BirtDateTime element and calling 6 set methods... </p>
<p> </p>
<p>And actually like I said in the 1st post, <em>this.setDisplayValue(vars["out_start"]getTime()) </em>actually sets and displays the correct date! The issue is that this is totally illogical because <em>vars["out_start"]getTime() </em>is not valid... Yet, when the variable which contains what appears to be a javascript date is followed by 'getTime()' method without the '.' in between, it displays the date correctly!</p>
JFreeman
<p>I do not believe it should take that may set calls.</p>
<p> </p>
<p>Is it possible for you to provide a sample report with a flat file data source I can run to better understand exactly what you are trying to accomplish?</p>
<p> </p>
<p>I can see about working up a modified version based on your sample.</p>
akk2
<p>Here is the test report with the csv. Making it, I noticed that displaying directly the variable date and not going through its OnCreate script works. Must have something to do with the <em>this.setDisplayValue(vars["date"])</em> bypassing code to convert the javascript date automatically... I might not have to use OnCreate on the data elements on my report.. (to confirm!) still I'd be very curious to see/know how it's done! Thanks!</p>
<p> </p>
<p>(btw, report has 3 columns, the 3rd column sets the date via the OnScript of the data element)</p>
akk2
<p>Actually, why it worked when there was an error in the script was that it most likely used the data element's expression instead and the OnCreate script! So the script was simply ignored, an error was displayed in the bottom of the report and the formula entered in the expression was used. </p>
<p> </p>
<p>So it had nothing to do with the OnScript "<em>this.setDisplayValue(vars["out_start"]getTime())</em>" doing anything to display correctly the date, it's actually the exact opposite, because it threw an error, it simply was not used to output the date! </p>
JFreeman
<p>Ahh, that definitely makes more sense.</p>
<p> </p>
<p>You can use a BIRT DateTime object with your report variable to set the value in onCreate like this:</p>
<pre class="_prettyXprint _lang-js">
this.setDisplayValue(BirtDateTime.date(BirtDateTime.year(vars["date"]), BirtDateTime.month(vars["date"]), BirtDateTime.day(vars["date"]), vars["date"].getHours(), vars["date"].getMinutes()));
</pre>
<p>Take a look at the attached modified version of your report with this code in place.</p>