# Convert string "date code" for month and year to proper date type

**URL:** <https://discourse.julialang.org/t/convert-string-date-code-for-month-and-year-to-proper-date-type/84451>\
**Category:** New to Julia\
**Tags:** dates\
**Created:** [July 19, 2022, 11:27am UTC](https://discourse.julialang.org/t/convert-string-date-code-for-month-and-year-to-proper-date-type/84451 "2022-07-19T11:27:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 19, 2022, 11:27am UTC](https://discourse.julialang.org/t/convert-string-date-code-for-month-and-year-to-proper-date-type/84451/1 "2022-07-19T11:27:27Z")

</div>

I have a data frame with where one of the columns contains a string “date code”. An example of this code is as follows:

“04\_22”

which indicates the 4th month of the year 2022.

“11\_23”

would indicate the 11th month of 2023.

Rather than working with this code in string, I would like to convert the column of string code to a column of proper date types. How can that be done?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [July 19, 2022, 11:38am UTC](https://discourse.julialang.org/t/convert-string-date-code-for-month-and-year-to-proper-date-type/84451/2 "2022-07-19T11:38:12Z")

</div>

```julia
julia> dStr = "04_22"
"04_22"

julia> m, y = split(dStr, '_')
2-element Vector{SubString{String}}:
 "04"
 "22"

julia> DateTime(2000 + parse(Int, y), parse(Int, m), 1)
2022-04-01T00:00:00

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 19, 2022, 11:41am UTC](https://discourse.julialang.org/t/convert-string-date-code-for-month-and-year-to-proper-date-type/84451/3 "2022-07-19T11:41:30Z")

</div>

Also:

```julia
vd = ["04_22","11_23"]
vd .= replace.(vd, "_" => "-20")
Date.(vd, "mm-yyyy")

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 19, 2022, 11:47am UTC](https://discourse.julialang.org/t/convert-string-date-code-for-month-and-year-to-proper-date-type/84451/4 "2022-07-19T11:47:24Z")

</div>

There is no need to use split/parse/replace here, just parse directly with a `DateFormat` (and then add 2000 years):

```julia
julia> Date("11_23", dateformat"mm_yy") + Year(2000)
2023-11-01

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 19, 2022, 11:49am UTC](https://discourse.julialang.org/t/convert-string-date-code-for-month-and-year-to-proper-date-type/84451/5 "2022-07-19T11:49:41Z")

</div>

That is nice. Would you know why the dot is not needed to broadcast the sum of the 2000 years with the vector?

```julia
vd = ["04_22","11_23"]
Date.(vd, "mm_yy") .+ Year(2000)

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 19, 2022, 11:53am UTC](https://discourse.julialang.org/t/convert-string-date-code-for-month-and-year-to-proper-date-type/84451/6 "2022-07-19T11:53:42Z")

</div>

> [@rafael.guerra](#):
>
> Would you know why the dot is not needed to sum the year 2000 to the vector?

Yes, that is not intended, but something that was forgotten to deprecate for Julia 1.0, see [Dates/src/deprecated.jl](https://github.com/JuliaLang/julia/blob/master/stdlib/Dates/src/deprecated.jl).

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 20, 2022, 6:33am UTC](https://discourse.julialang.org/t/convert-string-date-code-for-month-and-year-to-proper-date-type/84451/7 "2022-07-20T06:33:26Z")

</div>

> [@rafael.guerra](#):
>
> ```julia
> vd = ["04_22","11_23"]
> Date.(vd, dateformat"mm_yy") .+ Year(2000)
> 
> ```

That is very nice and intuitive. Suppose I knew that it was not the first of the month that the code indicated, but always the last day of the month, what would your solution look like then?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 20, 2022, 6:48am UTC](https://discourse.julialang.org/t/convert-string-date-code-for-month-and-year-to-proper-date-type/84451/8 "2022-07-20T06:48:30Z")

</div>

> [@Nash](#):
>
> but always the last day of the month

Maybe you could do:

```julia
Date.(vd, "mm_yy") .+ Year(2000) .+ Month(1) .- Day(1)

```

---

<div class="post-metadata">

**Author:** ![mgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgp/32/28599_2.png) [@mgp](https://discourse.julialang.org/u/mgp)\
**Post date:** [July 20, 2022, 1:19pm UTC](https://discourse.julialang.org/t/convert-string-date-code-for-month-and-year-to-proper-date-type/84451/9 "2022-07-20T13:19:47Z")

</div>

An alternative for the last day of the month is

```julia
@. Dates.lastdayofmonth(Date(vd, "mm_yy") + Year(2000))

```
