Converting Date-Time Format to Date Only: Removing Time from Field Values in Script

To convert a date-time value in the format “8/23/2023 12:00 am” to just the date format “8/23/2023”, you can use the following code:

Assuming FieldValue1 is the date in format: “8/23/2023 12:00 am”

let dateTimePattern = /^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2} [ap]m$/i;
if (dateTimePattern.test(fieldValue1)) {
fieldValue1 = convertToDateFormat(fieldValue1);
/**
* @description To change the date format
* @param {*} dateTimeValue
* @returns
*/
function convertToDateFormat(dateTimeValue) {
let dateParts = dateTimeValue.split(' ');
return dateParts[0];
}

Leave a comment

Your email address will not be published. Required fields are marked *