# Timex error in year

Hi:

I am using the Timex library where the dates are parsed as

``````date: item["Transaction Effective Date"] |> Timex.parse!("{M}/{D}/{YY}"),
``````

as part of a list of maps. However, when the year is before 2000, Timex converts to wrong value. For example,

9/27/99 is converted to

``````date: ~N[2099-09-27 00:00:00]
``````

How I do make Timex parse correctly?

Thanks,
Gani -

This type of logic probably has to be implemented yourself. How can Timex tell when it should be 1930 vs. 2030? You on the other hand know your data, so it should be simple enough to wrap the parse function and massage the year to the correct, fully qualified value before you pass it to Timex.

2 Likes

I am not sure how Timex would know if it is supposed to be in 20XX or 19XX.
It would probably be best to update your data (if you own it) to have the full year OR do some hacky thing like:

You can tweak `30` here to whatever your cutoff year is.

``````@spec parse_date(binary()) :: Date.t()
def parse_date(date) do
[month, day, year] = String.split(date, "/")

year =
case String.to_integer(year) do
value when value >= 100 -> value
value when value >= 30 -> value + 1900
value -> value + 2000
end

Date.new!(year, String.to_integer(month), String.to_integer(day))
end
``````

I assume you actually want a Date and not a NaiveDateTime returned?

2 Likes

Some versions of the `%y` format handle guessing the century, for instance:

The year within century (0β99). When a century is not otherwise specified, values in the range 69β99 refer to years in the twentieth century (1969β1999); values in the range 00β68 refer to years in the twenty-first century (2000β2068).

But thatβs not what Timex does:

``````iex(7)> Timex.parse("9/27/99", "%-m/%d/%y", Timex.Parse.DateTime.Tokenizers.Strftime)

{:ok, ~N[2099-09-27 00:00:00]}
``````
1 Like

Thank you. This works great.

gani-

1 Like

See you back here in 8 years

5 Likes