ShowTable of Contents
General Tips
While working with dates in server side javascript, I noticed some glichtes in Domino 8.5.0.
Here are some rules of thumb:
Trying to convert a text to date with @TextToTime() may work, but in my case it mixed day and month values. My solution: parse the string yourself into year, month and day parts and use @Date(y,m,d).
Note: make sure you give NUMBER values as parameter for @Date()! Otherwise @Date returns strange results like "31th dec 0001".
Getting the value of a date field with GetComponentValue() sometimes returns a string, and sometimes a javascript Date() object.
In one case it returned a Date() in the onchange event of that field. When getting the value in the onchange event of another field, GetComponetValue() returned a string.
Here is a little helper function to workaround that problem:
CAUTION: this function works in one locale only, in my case with german dates in the format DD.MM.YYYY.
function ynTextToDate(v) {
try {
if (typeof v != "string") return v;
var ty = Number(v.substring(v.lastIndexOf(".")+1));
var td = Number(v.substring(0, v.indexOf(".")));
var tm = Number(v.substring(v.indexOf(".")+1, v.lastIndexOf(".")));
return @Date(ty,tm,td);
} catch (e) {
print("ynTextToDate "+e);
}
}
Formatting Dates
Use I18n.toString(JavaDate, pattern) with these patterns:
Symbol Meaning Presentation Example
G era designator Text AD
y year Number 1996
M month in year Text or Number July (or) 07
d day in month Number 10
h hour in am/pm (1-12) Number 12
H hour in day (0-23) Number 0
m minute in hour Number 30
s second in minute Number 55
S fractional second Number 978
E day of week Text Tuesday
a am/pm marker Text PM
k hour in day (1-24) Number 24
K hour in am/pm (0-11) Number 0
z time zone Text Pacific Standard Time
Z time zone (RFC 822) Number -0800
v time zone (generic) Text Pacific Time
' escape for text Delimiter 'Date='
'' single quote Literal 'o''clock'
Examples
Pattern Formatted Text
"yyyy.MM.dd G 'at' HH:mm:ss vvvv" 1996.07.10 AD at 15:08:56 Pacific Time
"EEE, MMM d, ''yy" Wed, July 10, '96
"h:mm a" 12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, vvv" 0:00 PM, PT
"yyyyy.MMMMM.dd GGG hh:mm aaa" 01996.July.10 AD 12:08 PM
Auto format date and time in local format
format a date:
java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, context.getLocale()).format(date)
format a time:
java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT, context.getLocale()).format(date)
with date
as a Java date and java.textDateFormat.SHORT or .MEDIUM or .LONG or .FULL.
Get weekday
Thanks to Frank van der Linden
/** * Get the ISO week date week number
found at http://www.domino-weblog.nl/weblogs/domblog.nsf/d6plinks/FLIN-7WMGPX
*/
Date.prototype.getWeek = function () {
// Create a copy of this date object
var target = new Date(this.valueOf());
// ISO week date weeks start on monday
// so correct the day number
var dayNr = (this.getDay() + 6) % 7;
// Set the target to the thursday of this week so the
// target date is in the right year
target.setDate(target.getDate() - dayNr + 3);
// ISO 8601 states that week 1 is the week
// with january 4th in it
var jan4 = new Date(target.getFullYear(), 0, 4);
// Number of days between target date and january 4th
var dayDiff = (target - jan4) / 86400000;
// Calculate week number: Week 1 (january 4th) plus the
// number of weeks between target date and january 4th
var weekNr = 1 + Math.ceil(dayDiff / 7);
return weekNr;
}
Count No of days between Two dates
var startDate = document3.getItemValueDate("dtFromDate")
var endDate = document3.getItemValueDate("dtToDate")
var difference = endDate.getTime() - startDate.getTime();
difference = (Math.floor(difference/1000/60/60/24)) + 1;
Thanks, Arif Iqbal@TCSBangalore!