cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Format date to short date

oskarberntorp
12-Amethyst

Format date to short date

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;

 

 

 

 

ACCEPTED SOLUTION

Accepted Solutions
Constantine
17-Peridot
(To:jensc)

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

View solution in original post

3 REPLIES 3

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:

jensc_0-1678286833257.png

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

Constantine
17-Peridot
(To:jensc)

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.

Announcements


Top Tags