• May 18, 2026
  • Kishore Thutaram
  • 0

Requirement – default specific dates to UI field by using JS code

Steps : Controller.js file, function – onInit

//Requirment -  get Current Date
var oCurrDate = new Date();

//Requirment - First day of current month
var oFirstDayCurrentMonth = new Date(oCurrDate.getFullYear(), oCurrDate.getMonth(),1);

//Requirment -First day of last month
var oFirstDayLastMonth = new Date(oFirstDayCurrentMonth);
// Subtract one month to get the first day of the previous month
oFirstDayLastMonth.setMonth(oFirstDayCurrentMonth.getMonth() - 1);

//Requirment - get last day of previous month
//funcion
getLastDayOfPreviousMonth: function () {
var today = new Date(); // Get current date
today.setDate(1); // Set to the first day of the current month
today.setDate(today.getDate() - 1); // Subtract one day to get the last day of the previous month
return today; // Return the  date
}

//Requirment -get First Saturday of current month -- funcion
var CurrMonthSat = this.getFirstSaturdayOfCurrentMonth();
getFirstSaturdayOfCurrentMonth: function () {

var today = new Date();
// Set the date to the first day of the current month
var firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
// Get the day of the week for the first day (0=Sunday, ..., 6=Saturday)
var dayOfWeekOfFirstDay = firstDayOfMonth.getDay();
// Calculate how many days to add to get to the first Saturday
var daysToAdd = (6 - dayOfWeekOfFirstDay + 7) % 7;
// Add the calculated days to the first day of the month to get the first Saturday
firstDayOfMonth.setDate(firstDayOfMonth.getDate() + daysToAdd);
return firstDayOfMonth;
},

//Requirment - Get first Saturday of previous month - function
var prevMonth = new Date().getMonth() - 1; 
var PrevMonthSat = this.firstSaturdayPrev(prevMonth, curDateMonth.getFullYear());

firstSaturdayPrev: function (month, year) {
var tempDate = new Date();
tempDate.setHours(0, 0, 0, 0);
tempDate.setMonth(month);
tempDate.setYear(year);
tempDate.setDate(1);

var day = tempDate.getDay();
var toNextSat = day !== 0 ? 6 - day : 0;
tempDate.setDate(tempDate.getDate() + toNextSat);
return tempDate;			
}

//we can set the resultant date to the model, which will be binded to XML
Example:
this.getView().getModel("Data").setProperty("/date", PrevMonthSat);

Kishore Thutaram

SAP Solution Architect | 16+ Years' Experience in SAP | Sharing Practical SAP Knowledge | Engineering Graduate with Expertise in SAP Architecture

https://fiowelt.com