# How to Parse Date or Time in multiple possible format?

**URL:** <https://discourse.julialang.org/t/how-to-parse-date-or-time-in-multiple-possible-format/30851>\
**Category:** New to Julia\
**Created:** [November 8, 2019, 4:51am UTC](https://discourse.julialang.org/t/how-to-parse-date-or-time-in-multiple-possible-format/30851 "2019-11-08T04:51:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Chen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_chen/32/5992_2.png) [@Eric\_Chen](https://discourse.julialang.org/u/Eric_Chen)\
**Post date:** [November 8, 2019, 4:51am UTC](https://discourse.julialang.org/t/how-to-parse-date-or-time-in-multiple-possible-format/30851/1 "2019-11-08T04:51:55Z")

</div>

My user upload a CSV file with “Time” column  
Sometimes could be HHMMSS, sometimes could be HH:MM:SS, sometimes could be other format, like HHMM.

How can I read the CSV file and automatically convert the “Time” column to a Time object?

Can I parse it with something like: " HHMMSS|HH:MM:SS " → this is doable in C#.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 8, 2019, 5:00am UTC](https://discourse.julialang.org/t/how-to-parse-date-or-time-in-multiple-possible-format/30851/2 "2019-11-08T05:00:26Z")

</div>

you can remove `:` from all entries beforehand

or  
a `try` block using these:

```julia
date_object = try
        Date(blah, dateformat"HH:MM:SS")
    catch
        Date(blah, dateformat"HHMMSS")
    end
```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 8, 2019, 6:01am UTC](https://discourse.julialang.org/t/how-to-parse-date-or-time-in-multiple-possible-format/30851/3 "2019-11-08T06:01:23Z")

</div>

First, I don’t think this gives the desired result, eg

```julia
julia> Date("09:01:02", dateformat"HH:MM:SS")
0001-01-01

```

It would be better to use `Time`.

Second, I think that `try ... catch` is not recommended for use like this because it may be slow. It would be better to just test the string for `:`. Eg something like

```julia
using Dates
function robust_time_parse(str)
    if any(isequal(':'), str)
        Time(str, dateformat"HH:MM:SS")
    else
        Time(str, dateformat"HHMMSS")
    end
end
robust_time_parse("090101")
robust_time_parse("09:01:01")

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 8, 2019, 6:07am UTC](https://discourse.julialang.org/t/how-to-parse-date-or-time-in-multiple-possible-format/30851/4 "2019-11-08T06:07:59Z")

</div>

ah, right, `Date` only handles **dates** , of course. well but op also mentioned

> [@Eric\_Chen](#):
>
> HHMM.

so maybe also check length? this all depends on how regulated the user input is (which should be done at the front end)

---

<div class="post-metadata">

**Author:** ![Eric\_Chen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_chen/32/5992_2.png) [@Eric\_Chen](https://discourse.julialang.org/u/Eric_Chen)\
**Post date:** [November 8, 2019, 6:10am UTC](https://discourse.julialang.org/t/how-to-parse-date-or-time-in-multiple-possible-format/30851/5 "2019-11-08T06:10:21Z")

</div>

Thank you, but there maybe some other cases, like HHMM, HHMMSS. milisecond, or any other format.  
Ideally i want to have a generic solution, just like in C#, that I can do “xxx|yyy|zzz”.

Where Can I request such a feature to Julia developer?

Thank you

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 8, 2019, 6:26am UTC](https://discourse.julialang.org/t/how-to-parse-date-or-time-in-multiple-possible-format/30851/6 "2019-11-08T06:26:14Z")

</div>

> [@Eric\_Chen](#):
>
> Where Can I request such a feature to Julia developer?

There is nothing preventing you from doing this, but since this is just a simple programming exercise, I think you should just implement it.

For the general case, you will find `tryparse` helpful.
