Forum Discussion
Script changing dates to text
As variant you may replace dates with texts in place, it could be like
function main(workbook: ExcelScript.Workbook) {
// https://techcommunity.microsoft.com/discussions/excelgeneral/script-changing-dates-to-text/4468085
fnDateToText(workbook.getActiveWorksheet().getRange("E:E").getUsedRange())
fnDateToText(workbook.getActiveWorksheet().getRange("H:H").getUsedRange())
}
const fnDateToText = (columnRange: ExcelScript.Range) =>
{
const converted = columnRange.getValues().map(x => {
const scalar = x[0]
let dateAsText: number | string | boolean = null
if (typeof scalar === "number") {
const d = new Date(Math.round((scalar - 25569) * 86400 * 1000))
dateAsText =
d.getDate().toString().padStart(2, '0') + "/" +
(d.getMonth() + 1).toString().padStart(2, '0') + "/" +
d.getFullYear().toString()
} else { dateAsText = scalar }
return [dateAsText]
})
columnRange.setNumberFormatLocal("@")
columnRange.setValues(converted)
}Function converts numbers (which dates actually are) in the column into text with date converted to "dd/mm/yyy". And ignores other values in such column.
- dylanvanNov 10, 2025Copper Contributor
I wanted to thank you. This script works perfectly and even takes other values or empty cells into account. I was going to check those manually but now that's not even necessary.
I had to change the start to
function main(workbook: ExcelScript.Workbook) {
fnDateToText(workbook.getActiveWorksheet().getRange("D:D").getUsedRange())
fnDateToText(workbook.getActiveWorksheet().getRange("O:O").getUsedRange())
fnDateToText(workbook.getActiveWorksheet().getRange("P:P").getUsedRange())
}
Since it's 3 columns but other than that you are a life saver.