# Tryparse() how to "convert" nullables to normal type

**URL:** https://discourse.julialang.org/t/tryparse-how-to-convert-nullables-to-normal-type/3490
**Category:** General Usage
**Tags:** question
**Created:** [May 3, 2017, 6:33am UTC](https://discourse.julialang.org/t/tryparse-how-to-convert-nullables-to-normal-type/3490 "2017-05-03T06:33:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![strickek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/strickek/32/14885_2.png) [@strickek](https://discourse.julialang.org/u/strickek)
#### Post date: [May 3, 2017, 6:33am UTC](https://discourse.julialang.org/t/tryparse-how-to-convert-nullables-to-normal-type/3490/1 "2017-05-03T06:33:42Z")

</div>

I want to test if a string is in a date format and parse it.  
Becaus try … catch need a lot of time I use tryparse().  
But I need a normal date, no nullable date. At the moment I do It like this:

```julia
datenullable = tryparse(Dates.Date, "2017-01-15")
if !isnull(datenullable)
    parse(Dates.Date, "2017-01-15") 
    else
    println("no match return/do something else")
end

```

Is there a better way?

---

<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: [May 3, 2017, 6:39am UTC](https://discourse.julialang.org/t/tryparse-how-to-convert-nullables-to-normal-type/3490/2 "2017-05-03T06:39:21Z")

</div>

I was not aware of a `tryparse` method for dates, but in any case, see `get` for accessing the value inside the `Nullable`. You can also provide a default as the second argument.

---

<div class="post-metadata">

### Author: ![strickek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/strickek/32/14885_2.png) [@strickek](https://discourse.julialang.org/u/strickek)
#### Post date: [May 3, 2017, 12:45pm UTC](https://discourse.julialang.org/t/tryparse-how-to-convert-nullables-to-normal-type/3490/3 "2017-05-03T12:45:45Z")

</div>

Thank you - `get()` works. I think `tryparse()` for dates is new in Julia 0.6. The following function returns the parsed `date`, or `0000-01-01` if parse fails, it is fast in both cases:

```julia
function parsedate(ds, df)
    datenullable = tryparse(Dates.Date, ds, df)
    if isnull(datenullable)
        return Date(0)
        else
        return get(datenullable)
    end
end

```

```julia
ds = "12.12.2017"
df = DateFormat("dd.mm.yyyy")
parsedate(ds, dfs)

```

`2017-12-12`

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [May 3, 2017, 12:55pm UTC](https://discourse.julialang.org/t/tryparse-how-to-convert-nullables-to-normal-type/3490/4 "2017-05-03T12:55:20Z")

</div>

```julia
parsedate(ds, df) = get(tryparse(Dates.Date, ds, df), Date(0))

```

makes use of the optional second argument for `get`, and looks a bit better because of that. Might also be a bit faster, but I didn’t check.
