Strptime() equivalent?

Hi. I have a Drafts 5 action that, along other things, needs to convert a long date (“August 29, 2019”) into a short date more appropriate to use as a filename (“2019-08-29”).

In python I could use datetime.strptime() to turn the long date into a date object, the. Use strftime() to reformat it to a short date.

I see that JS in Drafts has strftime. Any suggestions for converting “August 29, 2019” into something strftime will recognize as a date?

Thanks!

Try the parse method in combination with strftime.

let dtNew = Date.parse("August 29, 2019");
let strFormattedNew = strftime(dtNew, "%Y-%m-%d");
alert(strFormattedNew);
1 Like

Fantastic — thank you!

Just for the record here, Drafts includes the Date.js library in it’s Javascript runtime automatically, so Date.parse is dramatically enhanced over the standard Javascript version.

A few examples:

Date.parse("t")                 // Returns today's date.
Date.parse("today")             // Returns today's date.
Date.parse("tomorrow")          // Returns tomorrow's date.
Date.parse("yesterday")         // Returns yesterday's date.

Date.parse("next friday")       // Returns the date of the next Friday.
Date.parse("last monday")       // Returns the date of the previous Monday.

More examples in the Date.js read me.

1 Like

Even better! Confusing that it seems to use the same namespace (if im using the term right).