Use Case:
In many enterprise applications and data integrations (like Oracle ERP, SAP, or banking systems), date fields often come in legacy formats like 05-Apr-23. These formats are not always compatible with APIs, databases, or modern UI components, which typically expect standardized formats like MM-dd-yyyy.
This utility function helps developers normalize date strings quickly and reliably, especially when working with data migration, report generation, or middleware transformations (e.g., Oracle Integration Cloud or Node.js-based services).
Example Scenario: You're processing HR data extracts that include hire dates in dd-MMM-yy format. Before loading this data into a cloud service or a database, you need to convert all dates to MM-dd-yyyy format to match the target system's expectations.
js code:
function convertDate(inputDate) {
const months = {
Jan: '01', Feb: '02', Mar: '03', Apr: '04',
May: '05', Jun: '06', Jul: '07', Aug: '08',
Sep: '09', Oct: '10', Nov: '11', Dec: '12'
};
const parts = inputDate.split('-'); // dd-MMM-yy
const day = parts[0].padStart(2, '0');
const month = months[parts[1]];
const shortYear = parts[2];
// Create a temporary date to extract century
const tempDate = new Date();
const fullYearPrefix = tempDate.getFullYear().toString().substring(0, 2); // e.g., "20"
const year = fullYearPrefix + shortYear;
var result = month + '-' + day + '-' + year;
//return `${month}-${day}-${year}`;
return result;
}
No comments:
Post a Comment