Table of Contents
Get Week Number From Date
Description:
Given the BO of a date field this function will return the week number the specified date is part of.
...
Code Block | ||
---|---|---|
| ||
// theDt - the business object of the date field (i.e. BOA, BO.F_Date1) app.getSharedData().getWeekNumberFromDate = function(theDt) { if(theDt !== "") { // Create a copy of this date object var t = new Date(theDt); // ISO week date weeks start on monday so correct the day number var dayNr = (t.getDay() + 6) % 7; // Set the target to the thursday of this week so the target date is in the right year t.setDate(t.getDate() - dayNr + 3); // ISO 8601 states that week 1 is the week with january 4th in it var jan4 = new Date(t.getFullYear(), 0, 4); // Number of days between target date and january 4th var dayDiff = (t - 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; } else { return ""; } } |
Get Days in Month
Description:
Given a date it will return the number of days in that month.
...
Code Block | ||
---|---|---|
| ||
app.getSharedData().daysInThisMonth = function( date ) { var calendarObj = new Array(); calendarObj.push({month: 0, days: 31}); calendarObj.push({month: 1, days: 28}); calendarObj.push({month: 2, days: 31}); calendarObj.push({month: 3, days: 30}); calendarObj.push({month: 4, days: 31}); calendarObj.push({month: 5, days: 30}); calendarObj.push({month: 6, days: 31}); calendarObj.push({month: 7, days: 31}); calendarObj.push({month: 8, days: 30}); calendarObj.push({month: 9, days: 31}); calendarObj.push({month: 10, days: 30}); calendarObj.push({month: 11, days: 31}); // Get the "non-Leap Year" number of days in the current month. var month = date.getMonth(); // Returns [0-11] var days = get( calendarObj, month ).days; // Add one day to February, if this is a Leap Year. if( month === "1" && app.getSharedData().isLeapYear( date ) ) { days++; } return days; } |
Add Hours to TimeStamp
Description:
Add number of hours to a timestamp
...
Code Block | ||
---|---|---|
| ||
// dateObj1 - The date field (i.e. BO.F_Date) // numOfHours - The number of hours to add app.getSharedData().addHoursToDate = function(dateObj1, numOfHours) { var d1 = dateObj1.getValue(); //get first date var d1_milli = d1.getTime(); return new Date(Math.ceil(d1_milli + (1000 * 60 * 60 * numOfHours))); } |
Add Days To Date
Description:
Add number of days to a date.
...
Code Block | ||
---|---|---|
| ||
// dateObj1 - The date field (i.e. BO.F_Date) // numOfDays - The number of days to add app.getSharedData().addDaysToDate = function(dateObj1, numOfDays) { var d1 = dateObj1.getValue(); //get first date var d1_milli = d1.getTime(); return new Date(Math.ceil(d1_milli + (1000 * 60 * 60 * 24 * numOfDays))); } |
Add Working Days To Date
Description:
This function will return a new date with the number of specified working days added, it will not include weekends when determining the final date. You will need to set that returned date into your desired field.
...
Code Block | ||
---|---|---|
| ||
app.getSharedData().isHoliday = function(d) { var r = false; var holidays = ["01-01","02-18","04-19","05-20","08-05","09-02"]; //set your holidays //check if any of the days is a holiday for(var holiday in holidays) { var h = new Date(get(holidays, holiday)+"-2020"); if(d.getMonth() == h.getMonth() && d.getDay() == h.getDay()) { r = true; } } return r; } // dateObj1 - The date field (i.e. BO.F_Date) // numOfDays - The number of working days to add app.getSharedData().addWorkingDaysToDate = function(dateObj1, numOfDays) { var sd = dateObj1.getValue(); //get first date var ed = dateObj1.getValue(); var counter = numOfDays; var millisecondsPerDay = 86400 * 1000; while (counter > 0) { ed = new Date(Math.ceil(ed.getTime() + millisecondsPerDay)); var theDay = ed.getDay(); //dont count sat and sunday and holidays if(theDay !== 0 && theDay !==6 && !app.getSharedData().isHoliday(ed)) { counter--; } } return ed; } |
Subtract Days From Date
Description:
Subtract number of days from a date
...
Code Block | ||
---|---|---|
| ||
// dateObj1 - The date field (i.e. BO.F_Date) // numOfDays - The number of days to subtract app.getSharedData().subtractDaysFromDate = function(dateObj1, numOfDays) { var d1 = dateObj1.getValue(); //get first date var d1_milli = d1.getTime(); return new Date(Math.ceil(d1_milli - (1000 * 60 * 60 * 24 * numOfDays))); } |
Subtract Working Days From Date
Description:
Subtract number of working days (not incl Sat or Sun) to a date.
...
Code Block | ||
---|---|---|
| ||
// dateObj1 - The date field (i.e. BO.F_Date) // numOfDays - The number of working days to subtract app.getSharedData().subtractWorkingDaysFromDate = function(dateObj1, numOfDays) { var d1 = dateObj1.getValue(); //get first date var d2 = dateObj1.getValue(); var counter = numOfDays; var millisecondsPerDay = 86400 * 1000; while (counter > 0) { d2 = new Date(Math.ceil(d2.getTime() - millisecondsPerDay)); var theDay = d2.getDay(); //dont count sat and sunday if(theDay !== 0 && theDay !==6) { counter--; } } return d2; } |
Convert AD Date to Buddhist Date
Description:
Convert a standard AD date into a Buddhist Date
...
Code Block | ||
---|---|---|
| ||
// dateAD: the date to convert app.getSharedData().convertToBuddhistDate = function(dateAD){ var dateBE = new Date(dateAD); dateBE.setFullYear(dateAD.getFullYear() + 543); return dateBE; } |
Calculate Age From Birth Date
Description:
Calculates the age from a specified birth date.
...
Code Block | ||
---|---|---|
| ||
// birthDay - the date to calculate the age from app.getSharedData().getAgeFromBirthDate = function(birthDay) { // 1557600000 is 24 * 3600 * 365.25 * 1000 Which is the length of a year // the length of a year is 365 days and 6 hours which is 0.25 day. // double tilde converts float to integer return ~~((Date.now() - birthDay) / (31557600000)); } |
Verify Entered Date is After Today
Description:
This function will set the date field to invalid if the entered date is not after today.
...
Code Block | ||
---|---|---|
| ||
app.getSharedData().isDateAfterToday(BO.F_Date1, "The date must be after today."); |
Verify Entered Date is Before Today
Description:
This function will set the date field to invalid if the entered date is not after today.
...
Code Block | ||
---|---|---|
| ||
app.getSharedData().isDateAfterToday(BO.F_Date1, "The date must be after today."); |
Compare Date Fields (D2 must be greater than D1)
Description:
Given two dates the function will validate that they are numOfDays days apart. If the dateObj2 is not numOfDays days greater than dateObj1 then dateObj2 will be set invalid with the specified errorMsg.
...
Code Block | ||
---|---|---|
| ||
app.getSharedData().compareDatesByDays(BO.F_Date2, BO.F_Date3, 14, "The date must be 14 days past D1."); |
Calculate Working Days Between Dates
Description:
Returns the number of calendar days between the two dates specified. The function expects to receive date objects, not strings.**Updated to account for holidays.
Usage Notes:
1. Copy the function to the Settings...Events...onStart section of your application.
...
Code Block | ||
---|---|---|
| ||
app.getSharedData().workingDaysBetweenDates = function(startDt, endDt, includeWeekends, excludeHolidays) { // Validate input if (endDt < startDt) return 0; // Calculate days between dates var millisecondsPerDay = 86400 * 1000; // Day in milliseconds startDt.setHours(0,0,0,1); // Start just after midnight endDt.setHours(23,59,59,999); // End just before midnight var diff = endDt.getTime() - startDt.getTime(); // Milliseconds between datetime objects var days = Math.ceil(diff / millisecondsPerDay); if(!includeWeekends) { // Subtract two weekend days for every week in between var weeks = Math.floor(days / 7); var days = days - (weeks * 2); // Handle special cases var startDay = startDt.getDay(); var endDay = endDt.getDay(); // Remove weekend not previously removed. if (startDay - endDay > 1) days = days - 2; // Remove start day if span starts on Sunday but ends before Saturday if (startDay === 0 && endDay !== 6) days = days - 1 // Remove end day if span ends on Saturday but starts after Sunday if (endDay === 6 && startDay !== 0) days = days - 1 } //remove any holidays that might be in the range if(excludeHolidays !== null) { for(var h=0; h<excludeHolidays.length;h++) { var th = get(excludeHolidays, h).d.getTime(); if(th < endDt.getTime() && th > startDt.getTime()) { days--; } } } return days; |
Get Month From Date
Description
Here is a function that when passed a date will return the month name.
...