Forms, fields and formulas

How do I convert text such as DD/MM/YYYY, or an epoch number, into a date?

Rearrange the text into year-first order with substring, then use dateFromText; for an epoch number, add the days to 1 January 1970.

How-to

Short answer: dateFromText reads year-first dates only. Rearrange day-first text into year/month/day with a helper field, then convert it.

Why it happens

  • Datefromtext expects the year first, as in Datefromtext("1970/05/29"). It has no day-first option.
  • An epoch number counts seconds from 1 January 1970, so it needs arithmetic, not a text conversion.

How to fix it

To convert DD/MM/YYYY text:

  1. Keep the text date in a text field, for example Date_in_text.
  2. Add a helper text field computed as concatenate(Date_in_text.substring(7,10), "/", Date_in_text.substring(4,5), "/", Date_in_text.substring(1,2)). This turns 29/05/1970 into 1970/05/29.
  3. In a Date field, use dateFromText(Helper_Field).

substring(start, end) counts from 1 and includes both ends. See text expressions.

To convert an epoch number:

  1. In a Date field, use Date(1970, 1, 1) + (Epoch_Field / 86400).rounddown(). Adding a number to a date adds days.
  2. If the value is in milliseconds, divide by 86400000 instead.

Important

If you validate text dates with a regex, remove any leading or trailing spaces from the expression.

Related answers