Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X
Hi,
When trying to format a date into MM-DD, I get the error "Invalid format: "2023-03-07 15:09:57" is too short". What am I doing wrong in my code bellow?
let datetime = parseDate(r.eventDesc1, "yyyy-MM-dd HH:mm:ss.SSS");
let formattedDate = dateFormat(r.eventDesc1);
r.eventDesc1 = formattedDate;
Solved! Go to Solution.
It is also worth mentioning that all those date formatting specs should include timezone as a safety precaution, e.g. "yyyy-MM-dd HH:mm:ss Z". I can't tell how many times I've seen it being a root cause for bugs.
/ Constantine
Hello,
Because you have the ".SSS" in your date parser, but not in your date string (if "2023-03-07 15:09:57" is your date string) the parseDate function will throw this error.
Either add the milliseconds to you date string, or remove the milliseconds in the date format.
Seems to me like you are missing an argument for the dateFormat function. The dateFormat function takes two arguments, dateValue and dateFormat:
See below example:
let datetime = parseDate("2023-03-07 15:09:57.00", "yyyy-MM-dd HH:mm:ss");
result = dateFormat(datetime, "MMdd");
This will result in a string that contains: 0307
And I think that is what you are trying to do so I hope this helps.
Regards,
Jens
It is also worth mentioning that all those date formatting specs should include timezone as a safety precaution, e.g. "yyyy-MM-dd HH:mm:ss Z". I can't tell how many times I've seen it being a root cause for bugs.
/ Constantine
@oskarberntorp You should accept Jens' extensive and correct answer. I just added a side note to it.