colName = Column Dialog("Select Date Column");
reformatDateColumn = Function({dt, colName},
dt = Current Data Table();
If(Column(dt, colName) == Empty(),
Throw("The selected column does not exist in the current data table.")
);
);
formatDate = Function({date},
If(Contains(date, "/"),
If(Substr(date, 3, 1) == "/",
date,
// Assume mm/dd/yyyy format and convert to dd/mm/yyyy
Concat(Word(2, date, "/"), "/", Word(1, date, "/"), "/", Word(3, date, "/"))
)
,
// Handle mmddyyyy, ddmmyyyy, yyyymmdd, or yyyyddmm format
If(Length(date) == 8,
// Check if first two digits form a valid month or day
If(Num(Substr(date, 1, 2)) > 12,
// Assume ddmmyyyy format
Concat(Substr(date, 1, 2), "/", Substr(date, 3, 2), "/", Substr(date, 5, 4))
,
// Assume mmddyyyy format and convert to dd/mm/yyyy
Concat(Substr(date, 3, 2), "/", Substr(date, 1, 2), "/", Substr(date, 5, 4))
)
,
If(Length(date) == 8,
// Check if first 4 digits form a valid year
If(Num(Substr(date, 1, 4)) > 999,
// Check if the next two digits form a valid month or day
If(Num(Substr(date, 5, 2)) > 12,
// Assume yyyyddmm format
Concat(Substr(date, 7, 2), "/", Substr(date, 5, 2), "/", Substr(date, 1, 4))
,
// Assume yyyymmdd format and convert to dd/mm/yyyy
Concat(Substr(date, 7, 2), "/", Substr(date, 5, 2), "/", Substr(date, 1, 4))
)
,
// If not a valid year or format, return the original date
date
)
,
// If the date length is not 8, return the original date (invalid format)
date
)
)
)
);
New Window("Data Processing Tools",
V List Box(
Button Box("Reformat Date Column",
// Open a dialog to select the date column
colName = Column Dialog("Select Date Column");
reformatDateColumn(Current Data Table(), colName);
)
)
);