# Converting date string to Ecto.Date

**URL:** <https://forum.elixirforum.com/t/converting-date-string-to-ecto-date/3809>\
**Category:** Questions\
**Tags:** ecto, phoenix, dates\
**Created:** [February 22, 2017, 9:14pm UTC](https://forum.elixirforum.com/t/converting-date-string-to-ecto-date/3809 "2017-02-22T21:14:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sacepums](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sacepums/32/4309_2.png) [@sacepums](https://forum.elixirforum.com/u/sacepums)\
**Post date:** [February 22, 2017, 9:14pm UTC](https://forum.elixirforum.com/t/converting-date-string-to-ecto-date/3809/1 "2017-02-22T21:14:52Z")

</div>

Hey guys.

I’m new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for example “22.02.2017” (dd.mm.yyyy) to Ecto.Date struct. How can I do that? Or at least to a “yyyy-mm-dd” string  
I come from PHPs background and PHP has strtotime() function, which converts any date format you can throw at it into timestamp… Is there anything like that in elixir / phoenix?

Thanks

---

<div class="post-metadata">

**Author:** ![murtza](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/murtza/32/8034_2.png) [@murtza](https://forum.elixirforum.com/u/murtza)\
**Post date:** [February 22, 2017, 9:59pm UTC](https://forum.elixirforum.com/t/converting-date-string-to-ecto-date/3809/2 "2017-02-22T21:59:47Z")

</div>

You can use `Ecto.Date.cast`. See [https://hexdocs.pm/ecto/Ecto.Date.html](https://hexdocs.pm/ecto/Ecto.Date.html) for reference.

---

<div class="post-metadata">

**Author:** ![sacepums](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sacepums/32/4309_2.png) [@sacepums](https://forum.elixirforum.com/u/sacepums)\
**Post date:** [February 22, 2017, 10:02pm UTC](https://forum.elixirforum.com/t/converting-date-string-to-ecto-date/3809/3 "2017-02-22T22:02:00Z")

</div>

But how can I turn that dd.mm.yyyy date to yyyy-mm-dd? What is the best practice? Cast the string into tuple?

---

<div class="post-metadata">

**Author:** ![murtza](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/murtza/32/8034_2.png) [@murtza](https://forum.elixirforum.com/u/murtza)\
**Post date:** [February 22, 2017, 10:07pm UTC](https://forum.elixirforum.com/t/converting-date-string-to-ecto-date/3809/4 "2017-02-22T22:07:27Z")

</div>

You can convert by using `String.replace("22.02.2017", ~r/\./, "-")` and then cast it to `Ecto.Date`

---

<div class="post-metadata">

**Author:** ![wfgilman](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/wfgilman/32/12467_2.png) [@wfgilman](https://forum.elixirforum.com/u/wfgilman)\
**Post date:** [February 22, 2017, 11:19pm UTC](https://forum.elixirforum.com/t/converting-date-string-to-ecto-date/3809/5 "2017-02-22T23:19:06Z")

</div>

You can also use Elixir’s [`Date`](https://hexdocs.pm/elixir/Date.html#content) module:

```
iex(11)> Date.from_iso8601!("2017-02-22")
~D[2017-02-22]

```

I believe Ecto.Date and DateTime are deprecated as of Ecto 2.1.
