2

What's the best/simplest way to parse a date string in dd-MM-YYYY format with AMPscript?

%%[

var @dateString1
var @date1

set @dateString1 = "21-08-2019"
set @date1 = DateParse(@dateString1)

]%%
dateString1: %%=v(@dateString1)=%%
<br>date1: %%=v(@date1)=%%

Output:

An error occurred when attempting to evaluate a DateParse function call. See inner exception for details. String was not recognized as a valid DateTime.

Also, from what I can tell, in addition to the DateParse function not handling it, this format is also not recognized by StringToDate, Format or FormatDate.

Anyone have any ideas?

  • 1
    The way you ask makes me think, that you do know a format which can be parsed. I believe this will most likely be MM/DD/YYYY. Couldn't you just transform the string with regexmatch into this format and parse it? Was just an idea. – Johannes Schapdick Aug 21 at 15:41
  • 1
    I would guess the issue is that SFMC parser is not set up to recognize that format as a viable date, so it is not able to parse it. Unlike formatdate() [output], DateParse() [input] does not have the option to describe the format the date is so you cannot express how to read the date. You will likely need to rebuild it into one of the date types that DateParse recognizes, like MM/DD/YYYY as @JohannesSchapdick said. – Gortonington Aug 21 at 16:53
  • 1
    @JohannesSchapdick, yes, I can and probably will do something like that, but it seems like there should be some shorter way to go about it. – Adam Spriggs Aug 21 at 17:49
  • Best I could offer is to create a function to do this for you (in AMPscript this would be a 'function' - e.g. A contentBlock that you call in that contains the repeatable script). This way you won't need to do the regex or substring, indexof, etc. each time – Gortonington Aug 21 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.