Extracting ISO dates from a string


We’ve started experimenting with ChatGPT to solve some of our development headaches and to improve accuracy in some of our tools.

We have an app where we receive strings from customers, these strings should contain a date and could be in different languages.

Here’s a few english example strings.

Question: When do you wish to travel?

  • I wish to travel today
  • Tomorrow
  • On Saturday
  • Sunday, 24th september

We’re expecting ChatGPT to return in the format YYYY-MM-DD and we have a simple test that we think works OK but surely can be improved, or adapter to a better practice.

Here’s a sample string we feed ChatGPT, model gpt-3.5-turbo
“Today’s date is 2023-09-22. Language is English. Extract the date from the following text ‘Tomorrow’. Return the result in JSON”

Result: {“date”: “2023-09-23”}

Thanks for your valuable input, suggestions, improvements etc.

Hi and welcome to the Developer Forum!

If you ask the model to output an ISO-Date format date in JSON, it will do so, assuming the date in the string contains enough information to do so, also use a low temperature, preferably 0 for these kinds of tasks.


I will look into the temperature parameter. But overall I am not doing anything stupid or out-of-line to perform what I want? Also in regards to spent tokens since there are costs associated to the requests.

Not at all, one of the AI’s primary commercial applications currently is converting unstructured data into structured, a well structured prompt with a low temperature is often at or above the 95th percentile in terms of accuracy, and can be further improved with some traditional code checking to ensure that a valid date has been generated.

We can go into details if you have some specific example input strings you wish to get parsed.

I’ve written up an idea before that might be useful, and this was particularly oriented to someone that wasn’t getting the strict date required for their API functions such as setting appointments.

There are many problems the AI might have, even if you feed it today’s date in your system prompt. Can it accurately give the date of next Saturday?

If not, you’d better tell the AI that it can’t - and you can provide the AI with some functions that it can call on-demand that can do these calculations precisely.

Function ideas from another post of mine:

“name”: “day_of_week”,
“description”: “Calculates the name of the day of the week (example: “Wednesday”) when provided a calendar date.”

Or simply let AI get a calendar that it wouldn’t be able to replicate itself, and then it could reliably figure out this stuff.

“name”: “get_monthly_calendar”
“description”: “returns a graphical calendar of the whole month containing a particular date, so AI can understand the month’s days of the week”

In general, my advice would be to not fight the model.

You can never guarantee it will 100% perfectly follow the instructions every time. So, don’t fight it.

Rather, build your system around the idea that the date may come back in another format and adjust it as needed.

This has many benefits including,

  • You never need to worry about it not working
  • You don’t need to waste any tokens or context trying to force the model to do something a particular way