Skip to main content
13-Aquamarine
March 8, 2023
Solved

Format date to short date

  • March 8, 2023
  • 2 replies
  • 2953 views

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;

 

 

 

 

Best answer by Constantine

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

2 replies

17-Peridot
March 8, 2023

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

18-Opal
March 9, 2023

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

18-Opal
March 9, 2023

@oskarberntorp You should accept Jens' extensive and correct answer. I just added a side note to it.