# Allow multiple datetime formats with CSV.jl

**URL:** <https://discourse.julialang.org/t/allow-multiple-datetime-formats-with-csv-jl/120891>\
**Category:** Data\
**Created:** [October 4, 2024, 6:55am UTC](https://discourse.julialang.org/t/allow-multiple-datetime-formats-with-csv-jl/120891 "2024-10-04T06:55:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [October 4, 2024, 6:55am UTC](https://discourse.julialang.org/t/allow-multiple-datetime-formats-with-csv-jl/120891/1 "2024-10-04T06:55:30Z")

</div>

I would like to read CSV files with a time column where the format of the time can be one of several alternatives. CSV.file() does not automatically recognize the formats.

Now I do like follows but is there are better way?

```julia
using Dates, DataFrames, CSV

formats = ["yyyy-mm-dd HH:MM:SS", "yyyy-mm-ddTHH:MM:SS"]

    # read file
    cf = DataFrame(CSV.File(filename, missingstring = "NA", types = Dict(:time => String))) 
    
    # convert time column
    for fmt in formats
        try
            cf.time = DateTime.(cf.time, DateFormat(fmt))
            break # if success, exit the loop
        catch e
            if isa(e, ArgumentError) || isa(e, MethodError)
                continue
            else
                rethrow(e) # Re-throw the exception if it's not the type I expected
            end
        end
    end

```
