Date and time handling is slightly complex in all Programming Languages, since each area has different date/time format and each language has its own way of handling these objects. In iReport, many developers finds difficulty in formatting, treating date and time in day-to-day operations. I am listing here some of the useful tricks/methods to be used in iReport:
new Date()
new Date( (new Date()).getYear(),(new Date()).getMonth(), (new Date()).getDate())
$F{ThisDate} == null
$F{ThisDate} == null ? new Date( (new Date()).getYear(),(new Date()).getMonth(), (new Date()).getDate()): $F{ThisDate}
(int) ((($F{OutTime}==null? (new Date( (new Date()).getYear(),(new Date()).getMonth(), (new Date()).getDate())).getTime(): (new Date($F{OutTime}.getYear(),$F{OutTime}.getMonth(), $F{OutTime}.getDate()).getTime())) - (new Date($F{InTime}.getYear(),$F{InTime}.getMonth(), $F{InTime}.getDate())).getTime())/ (1000 * 60 * 60 * 24))+1
where, getTime translates the date into milliseconds which allows to add subtract days.
To convert the result from milliseconds to days we divide the result by (1000 * 60 * 60 * 24).
To include the start date in difference we added 1. In beginning of the expression, we used "(int)" to cast the output to integer value.
I hope that the above information may be useful to you. Please feel free to comment, if you find any correction or you have a better solution.
Thanks!
1. To get the current date and time:
new Date()
2. To get the current date only (Today in .Net):
new Date( (new Date()).getYear(),(new Date()).getMonth(), (new Date()).getDate())
3. To check if a variable / date is null?:
$F{ThisDate} == null
4. In development, there arise some situations in which we have to use the current date if a date is null, then use a variable and assign it the following expression:
$F{ThisDate} == null ? new Date( (new Date()).getYear(),(new Date()).getMonth(), (new Date()).getDate()): $F{ThisDate}
5. To get the difference between two dates (including start and end date) in "No. of Days" unit:
(int) ((($F{OutTime}==null? (new Date( (new Date()).getYear(),(new Date()).getMonth(), (new Date()).getDate())).getTime(): (new Date($F{OutTime}.getYear(),$F{OutTime}.getMonth(), $F{OutTime}.getDate()).getTime())) - (new Date($F{InTime}.getYear(),$F{InTime}.getMonth(), $F{InTime}.getDate())).getTime())/ (1000 * 60 * 60 * 24))+1
where, getTime translates the date into milliseconds which allows to add subtract days.
To convert the result from milliseconds to days we divide the result by (1000 * 60 * 60 * 24).
To include the start date in difference we added 1. In beginning of the expression, we used "(int)" to cast the output to integer value.
I hope that the above information may be useful to you. Please feel free to comment, if you find any correction or you have a better solution.
Thanks!