Date-based navigation (roll days, months) in BIAL script directly, format days to own formats.
Link to blog: Design Studio SDK: Date Object Component
Name | Description | Group | Default | Visible |
---|
Name | Description | Group | Default | Visible |
---|
Name | Parameters | Return |
---|---|---|
format [details] | formatPattern | String |
formatWithMask [details] | mask, (utc) | String |
getCalendarWeek [details] | int | |
getCurrentTime [details] | String | |
getDay [details] | int | |
getDayName [details] | String | |
getDayOfWeek [details] | int | |
getDayOfYear [details] | int | |
getLastDayOfMonth [details] | (formatPattern), (month), (year) | String |
getMonth [details] | int | |
getMonthName [details] | String | |
getQuarter [details] | int | |
getTimezoneOffset [details] | String | |
getYear [details] | int | |
initialize [details] | year, month, day | void |
initializeWithInternalDate [details] | internalDate | void |
internal_initializeNow [details] | void | |
internal_toString [details] | String | |
private_checkPrototypeFormat [details] | void | |
private_checkPrototypeRecalculate [details] | void | |
rollDays [details] | days | void |
rollMonths [details] | months | void |
rollWeeks [details] | weeks | void |
rollYears [details] | years | void |
toLocaleString [details] | String | |
toUTCString [details] | String |
Name | Description | Group | Default | Visible |
---|---|---|---|---|
days [String] |
Days |
Display | false | |
oneDay [String] |
One Day |
Display | false |
String format |
Format by primitive string pattern which supports following content YYYY: year YY: 2-digit year MM: 2-digit M: month DD: 2-digit day D: day WW: 2-digit calendar week W: calendar week Q: quarter
|
||||||||||||
Source Code |
this.internal_initializeNow(); this.date.recalculate(); var resultString = formatPattern; resultString = resultString.replace("YYYY", this.getYear()); resultString = resultString.replace("YY", this.getYear() % 100); if(this.getMonth() < 10) { resultString = resultString.replace("MM", "0" + this.getMonth()); resultString = resultString.replace("M", this.getMonth()); } else { resultString = resultString.replace("MM", this.getMonth()); } if(this.getDay() < 10) { resultString = resultString.replace("DD", "0" + this.getDay()); resultString = resultString.replace("D", this.getDay()); } else { resultString = resultString.replace("DD", this.getDay()); } if(this.getCalendarWeek() < 10) { resultString = resultString.replace("WW", "0" + this.getCalendarWeek()); resultString = resultString.replace("W", this.getCalendarWeek()); } else { resultString = resultString.replace("WW", this.getCalendarWeek()); } resultString = resultString.replace("QQQ", "00" + this.getQuarter()); resultString = resultString.replace("QQ", "0" + this.getQuarter()); resultString = resultString.replace("Q", this.getQuarter()); return resultString; |
||||||||||||
String formatWithMask |
Format by full mask. Commom Masks: default: "ddd mmm dd yyyy HH:MM:ss", shortDate: "m/d/yy", mediumDate: "mmm d, yyyy", longDate: "mmmm d, yyyy", fullDate: "dddd, mmmm d, yyyy", shortTime: "h:MM TT", mediumTime: "h:MM:ss TT", longTime: "h:MM:ss TT Z", isoDate: "yyyy-mm-dd", isoTime: "HH:MM:ss", isoDateTime: "yyyy-mm-dd'T'HH:MM:ss", isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'" You want to format the date to get "Wed 04, 2015" back var mask = "ddd mm, yyyy"; The "formatted" variable has now the requested format.var formatted = DATE.formatWithMask(mask, false);
|
||||||||||||
Source Code |
this.internal_initializeNow(); this.date.recalculate(); return this.date.format(mask, utc); |
||||||||||||
int getCalendarWeek |
Returns calendar week (iso form) |
||||||||||||
Source Code |
this.internal_initializeNow(); this.date.recalculate(); return this.calendarWeek; |
||||||||||||
String getCurrentTime |
Get current time (HH:MM:SS:SSS) |
||||||||||||
Source Code |
var d = new Date(); var timeString = d.getHours()+":"+d.getMinutes()+":"+d.getSeconds()+"."+d.getMilliseconds(); return timeString; |
||||||||||||
int getDay |
Returns the day of month |
||||||||||||
Source Code |
this.internal_initializeNow(); return this.date.getUTCDate(); |
||||||||||||
String getDayName |
Returns day name in English |
||||||||||||
Source Code |
this.internal_initializeNow(); this.date.recalculate(); return this.dayOfWeekName; |
||||||||||||
int getDayOfWeek |
Returns day of week as integer (0-based, 0 is Sunday, 1 is Monday) |
||||||||||||
Source Code |
this.internal_initializeNow(); return this.date.getUTCDay(); |
||||||||||||
int getDayOfYear |
Returns day of year as integer |
||||||||||||
Source Code |
this.internal_initializeNow(); this.date.recalculate(); return this.daysFromFirstJanuary; |
||||||||||||
String getLastDayOfMonth |
Determine last day of a given month. You can either specify the month and optionally the year yourself or use the initialized date right away. To do that don't pass a month or a year.
|
||||||||||||
Source Code |
var lastDay = null; if(month){ if(month.indexOf("0") == 0) { month = month.substring(1); } month = parseInt(month); if(year){ this.initialize(parseInt(year), month + 1, 0); }else{ this.initialize(this.getYear(), month + 1, 0); } }else{ this.initialize(this.getYear(), this.getMonth()+1, 0); } if(formatPattern){ return this.format(formatPattern); } return this.format("YYYYMMDD"); |
||||||||||||
int getMonth |
returns month, 1-index based (1-12) |
||||||||||||
Source Code |
this.internal_initializeNow(); return this.date.getUTCMonth() + 1; |
||||||||||||
String getMonthName |
Returns month name in English |
||||||||||||
Source Code |
this.internal_initializeNow(); this.date.recalculate(); return this.monthName; |
||||||||||||
int getQuarter |
Returns quarter as integer (1-4) |
||||||||||||
Source Code |
this.internal_initializeNow(); this.date.recalculate(); return this.quarter; |
||||||||||||
String getTimezoneOffset |
Returns the offset |
||||||||||||
Source Code |
this.internal_initializeNow(); return this.date.getTimezoneOffset(); |
||||||||||||
int getYear |
Returns year as integer |
||||||||||||
Source Code |
this.internal_initializeNow(); return this.date.getUTCFullYear(); |
||||||||||||
void initialize |
initialization method with separate parameters
|
||||||||||||
Source Code |
this.year = year; this.month = month - 1; this.day = day; this.date = new Date(); var that = this; // create a special initialization function for UTC time zone to avoid calculation inconsistencies Date.prototype.createUtc = function(year, month0, day) { this.setUTCFullYear(year); this.setUTCMonth(month0); this.setUTCDate(day); this.setUTCHours(0); this.setUTCMinutes(0); this.setUTCSeconds(0); this.setUTCMilliseconds(0); } this.private_checkPrototypeRecalculate(); this.private_checkPrototypeFormat(); this.date.createUtc(this.year, this.month, this.day); this.date.recalculate(); |
||||||||||||
void initializeWithInternalDate |
initialization method with internal date notation
|
||||||||||||
Source Code |
this.internalDate = internalDate; // the most primitive way to get integers from the internal date string var year = parseInt(internalDate.substring(0,4)); var monthString = internalDate.substring(4,6); if(monthString.indexOf("0") == 0) { monthString = monthString.substring(1); } var month = parseInt(monthString); var dayString = internalDate.substring(6,8); if(dayString.indexOf("0") == 0) { dayString = dayString.substring(1); } var day = parseInt(dayString); // APPLICATION.createInfoMessage("Init: " + year + "," + month + ", " + day); this.initialize(year, month, day); |
||||||||||||
void internal_initializeNow |
|||||||||||||
Source Code |
if(!this.date) { APPLICATION.createWarningMessage("Date Object Not Initialized. Initializing with some date. Use 'initialize' call next time."); this.date = new Date(); this.initialize(this.date.getUTCFullYear(), this.date.getUTCMonth() + 1, this.date.getUTCDay()); this.date.recalculate(); } |
||||||||||||
String internal_toString |
|||||||||||||
Source Code |
this.internal_initializeNow(); this.date.recalculate(); return "Todays Date: " + this.date.toUTCString() + "\n" + "Last Monday Date: " + this.lastMondayDate.toUTCString() + "\n\n" + "First January Date: " + this.firstJanuaryDate.toUTCString() + "\n" + "Monday Before First January Date: " + this.mondayBeforeFirstJanuaryDate.toUTCString() + "\n" + "Monday After First January Date: " + this.mondayAfterFirstJanuaryDate.toUTCString() + "\n\n" + "Today Day: " + this.dayOfWeekToday + "\n" + "First January Day: " + this.dayOfWeekFirstJanuary + "\n" + "Last Monday Day: " + this.lastMondayDate.getDay() + "\n\n" + "Date Serialization: " + this.date + "\n" + "Monday Before First January Serialization: " + this.mondayBeforeFirstJanuaryDate + "\n" + "Monday After First January Serialization: " + this.mondayAfterFirstJanuaryDate + "\n" + "daysFromLastMondayToMondayBeforeFirstJanuary: " + this.daysFromLastMondayToMondayBeforeFirstJanuary + "\n" + "daysFromLastMondayToMondayAfterFirstJanuary: " + this.daysFromLastMondayToMondayAfterFirstJanuary + "\n\n" + "Day of Year: " + this.getDayOfYear() + "\n" + "Day of Month: " + this.getDay() + "\n\n" + "Day of Week: " + this.getDayOfWeek() + "\n" + "Calendar Week: " + this.getCalendarWeek() + "\n" + "Month: " + this.getMonth() + "\n" + "Quarter: " + this.getQuarter() + "\n" + "Year: " + this.getYear() + "\n" + "TimeZoneOffset: " + this.getTimezoneOffset() + "\n" + "Day Name: " + this.getDayName() + "\n" + "Month Name: " + this.getMonthName() + "\n" + "Formatted: " + this.getDay() + "." + this.getMonth() + "." + this.getYear() + "\n" + "Formatted [DD.MM.YYYY WW]: " + this.format("DD.MM.YYYY WW") + "\n" + "Formatted [MM/DD/YYYY WW]: " + this.format("MM/DD/YYYY WW") + "\n" + "Formatted by Mask [default, utc]: " + this.formatWithMask(this.dateFormat.masks.default, true) + "\n" + "Formatted by Mask [isoDateTime, no utc]: " + this.formatWithMask(this.dateFormat.masks.isoDateTime, false) + "\n\n" + "Internal Year: " + this.year + "\n" + "Internal Month: " + this.month + "\n" + "Internal Day: " + this.day + "\n" + "Internal Date: " + this.internalDate + "\n" + ""; |
||||||||||||
void private_checkPrototypeFormat |
Private - do not call |
||||||||||||
Source Code |
var that = this; /* * Date Format 1.2.3 * (c) 2007-2009 Steven Levithan <stevenlevithan.com> * MIT license * * Includes enhancements by Scott Trenda <scott.trenda.net> * and Kris Kowal <cixar.com/~kris.kowal/> * * Accepts a date, a mask, or a date and a mask. * Returns a formatted version of the given date. * The date defaults to the current date/time. * The mask defaults to dateFormat.masks.default. */ //if (!Date.prototype.format) { // Date.prototype.format = function() { if (!this.date.format) { this.date.format = function() { var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g, timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g, timezoneClip = /[^-+\dA-Z]/g, pad = function(val, len) { val = String(val); len = len || 2; while (val.length < len) val = "0" + val; return val; }; var date = that.date; // Regexes and supporting functions are cached through closure return function(mask, utc) { var dF = that.dateFormat; // You can't provide utc if you skip other args (use the "UTC:" mask prefix) if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) { mask = date; date = undefined; } mask = String(dF.masks[mask] || mask || dF.masks["default"]); // Allow setting the utc argument via the mask if (mask.slice(0, 4) == "UTC:") { mask = mask.slice(4); utc = true; } var _ = utc ? "getUTC" : "get", d = date[_ + "Date"](), D = date[_ + "Day"](), m = date[_ + "Month"](), y = date[_ + "FullYear"](), H = date[_ + "Hours"](), M = date[_ + "Minutes"](), s = date[_ + "Seconds"](), L = date[_ + "Milliseconds"](), o = utc ? 0 : date.getTimezoneOffset(), flags = { d: d, dd: pad(d), ddd: dF.i18n.dayNames[D], dddd: dF.i18n.dayNames[D + 7], m: m + 1, mm: pad(m + 1), mmm: dF.i18n.monthNames[m], mmmm: dF.i18n.monthNames[m + 12], yy: String(y).slice(2), yyyy: y, h: H % 12 || 12, hh: pad(H % 12 || 12), H: H, HH: pad(H), M: M, MM: pad(M), s: s, ss: pad(s), l: pad(L, 3), L: pad(L > 99 ? Math.round(L / 10) : L), t: H < 12 ? "a" : "p", tt: H < 12 ? "am" : "pm", T: H < 12 ? "A" : "P", TT: H < 12 ? "AM" : "PM", Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""), o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4), S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10] }; return mask.replace(token, function($0) { return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1); }); }; }(); } // initialize once if(!that.dateFormat) { that.dateFormat = {}; } if (!that.dateFormat.masks) { // Some common format strings that.dateFormat.masks = { "default": "ddd mmm dd yyyy HH:MM:ss", shortDate: "m/d/yy", mediumDate: "mmm d, yyyy", longDate: "mmmm d, yyyy", fullDate: "dddd, mmmm d, yyyy", shortTime: "h:MM TT", mediumTime: "h:MM:ss TT", longTime: "h:MM:ss TT Z", isoDate: "yyyy-mm-dd", isoTime: "HH:MM:ss", isoDateTime: "yyyy-mm-dd'T'HH:MM:ss", isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'" }; } if (!that.dateFormat.i18n) { // Internationalization strings that.dateFormat.i18n = { dayNames: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], monthNames: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] }; }; |
||||||||||||
void private_checkPrototypeRecalculate |
Private - do not call |
||||||||||||
Source Code |
var that = this; // create a recalculate function which calculates information not available in JS date //if (!Date.prototype.recalculate) { // Date.prototype.recalculate = function() { if (!this.date.recalculate) { this.date.recalculate = function() { // exit if date did not changed var currentDate = that.date.toUTCString(); if (currentDate == that.currentCalculatedDate) { return; } that.currentCalculatedDate = currentDate; that.firstJanuaryDate = new Date(); // unfortunately SDK (or Rhino) has problems with dynamic creation of methods, need to check here again Date.prototype.createUtc = function(year, month0, day) { this.setUTCFullYear(year); this.setUTCMonth(month0); this.setUTCDate(day); this.setUTCHours(0); this.setUTCMinutes(0); this.setUTCSeconds(0); this.setUTCMilliseconds(0); } // create first january that.firstJanuaryDate.createUtc(that.getYear(), 0, 1); // what week day is first january? - need for ISO week calculation that.dayOfWeekFirstJanuary = that.firstJanuaryDate.getDay(); // days are 0-based, starting Sunday // 0 Sun // 1 Mon // 2 Tue // 3 Wed // 4 Thu // 5 Fri // 6 Sat // what week day is today? that.dayOfWeekToday = that.getDayOfWeek(); // substract until last monday that.lastMondayDate = new Date(); that.lastMondayDate.createUtc(that.getYear(), that.getMonth() - 1, that.getDay()); if (that.dayOfWeekToday > 1) { // Tue - Sat that.lastMondayDate.setDate(that.date.getDate() - (that.dayOfWeekToday - 1)); // substract day to catch Monday } else if (that.dayOfWeekToday < 1) { // Sun that.lastMondayDate.setDate(that.date.getDate() - 6); // substract day to catch Monday } // what week day is last Monday? - must be 1, just for debug that.dayOfWeekLastMonday = that.lastMondayDate.getDay(); // calculate mondays before and after first january that.mondayBeforeFirstJanuaryDate = new Date(); that.mondayBeforeFirstJanuaryDate.createUtc(that.firstJanuaryDate.getYear(), that.firstJanuaryDate.getMonth(), that.firstJanuaryDate.getDate()); that.mondayAfterFirstJanuaryDate = new Date(); that.mondayAfterFirstJanuaryDate.createUtc(that.firstJanuaryDate.getYear(), that.firstJanuaryDate.getMonth(), that.firstJanuaryDate.getDate()); if (that.dayOfWeekFirstJanuary == 0) { // Sunday that.mondayBeforeFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() - 6); that.mondayAfterFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() + 1); } else if (that.dayOfWeekFirstJanuary == 1) { // Monday that.mondayBeforeFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate()); that.mondayAfterFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate()); } else if (that.dayOfWeekFirstJanuary == 2) { // Tuesday that.mondayBeforeFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() - 1); that.mondayAfterFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() + 6); } else if (that.dayOfWeekFirstJanuary == 3) { // Wednesday that.mondayBeforeFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() - 2); that.mondayAfterFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() + 5); } else if (that.dayOfWeekFirstJanuary == 4) { // Thursday that.mondayBeforeFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() - 3); that.mondayAfterFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() + 4); } else if (that.dayOfWeekFirstJanuary == 5) { // Friday that.mondayBeforeFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() - 4); that.mondayAfterFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() + 3); } else if (that.dayOfWeekFirstJanuary == 6) { // Saturday that.mondayBeforeFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() - 5); that.mondayAfterFirstJanuaryDate.setDate(that.firstJanuaryDate.getDate() + 2); } // now we can count also day number in this year var milisecondsInADay = 60 * 60 * 24 * 1000; that.daysFromFirstJanuary = ((that.date - that.firstJanuaryDate) / milisecondsInADay) + 1; // count days between last monday and the mondays around first january that.daysFromLastMondayToMondayBeforeFirstJanuary = ((that.lastMondayDate - that.mondayBeforeFirstJanuaryDate) / milisecondsInADay); that.daysFromLastMondayToMondayAfterFirstJanuary = ((that.lastMondayDate - that.mondayAfterFirstJanuaryDate) / milisecondsInADay); // ISO 8601: First Week is the week with Thurdsay // count number of weeks, by substracting the mondays and dividing by 7 if (that.dayOfWeekFirstJanuary > 4 || that.dayOfWeekFirstJanuary == 0) { that.calendarWeek = (that.daysFromLastMondayToMondayAfterFirstJanuary / 7) + 1; } else { that.calendarWeek = (that.daysFromLastMondayToMondayBeforeFirstJanuary / 7) + 1; } // for now simplification, actually last year could have 53 calendar weeks by ISO 8601 if (that.calendarWeek == 0) { var lastYear = that.getYear() - 1; // exceptions are manually maintained for 1970 - 2054 if (lastYear == 1970 || lastYear == 1981 || lastYear == 1987 || lastYear == 1998 || lastYear == 2009 || lastYear == 2015 || lastYear == 2026 || lastYear == 2037 || lastYear == 2043 || lastYear == 2054) { // http://en.wikipedia.org/wiki/Common_year_starting_on_Thursday that.calendarWeek = 53; } else if (lastYear == 1920 || lastYear == 1948 || lastYear == 1976 || lastYear == 2004 || lastYear == 2060 || lastYear == 2088) { // http://en.wikipedia.org/wiki/Leap_year_starting_on_Thursday that.calendarWeek = 53; } else if (lastYear == 1992 || lastYear == 2020 || lastYear == 2048 || lastYear == 2076) { // http://en.wikipedia.org/wiki/Leap_year_starting_on_Wednesday that.calendarWeek = 53; } else { that.calendarWeek = 52; } } // for now names only in EN that.dayOfWeekName = that.dateFormat.i18n.dayNames[that.getDayOfWeek() + 7]; that.monthName = that.dateFormat.i18n.monthNames[that.getMonth() - 1 + 12]; // quarter that.quarter = Math.ceil(that.getMonth() / 3); }; }; |
||||||||||||
void rollDays |
rolls days up or down, -20 means down by 20 days
|
||||||||||||
Source Code |
this.internal_initializeNow(); this.date.setUTCDate(this.date.getUTCDate() + days); this.date.recalculate(); |
||||||||||||
void rollMonths |
rolls months up or down, -20 means down by 20 months
|
||||||||||||
Source Code |
this.internal_initializeNow(); this.date.setUTCMonth(this.date.getUTCMonth() + months); this.date.recalculate(); |
||||||||||||
void rollWeeks |
rolls weeks up or down, -20 means down by 20 weeks
|
||||||||||||
Source Code |
this.internal_initializeNow(); this.date.setUTCDate(this.date.getUTCDate() + (weeks*7)); this.date.recalculate(); |
||||||||||||
void rollYears |
rolls years up or down, -20 means down by 20 years
|
||||||||||||
Source Code |
this.internal_initializeNow(); this.date.setUTCFullYear(this.date.getUTCFullYear() + years); this.date.recalculate(); |
||||||||||||
String toLocaleString |
Returns locale string of the date |
||||||||||||
Source Code |
this.internal_initializeNow(); return this.date.toLocaleString() ; |
||||||||||||
String toUTCString |
returns UTC string of the date |
||||||||||||
Source Code |
this.internal_initializeNow(); return this.date.toUTCString() ; |
||||||||||||