# How to change date format and keep the type of variable?

**URL:** https://discourse.julialang.org/t/how-to-change-date-format-and-keep-the-type-of-variable/12194
**Category:** Performance
**Created:** [July 5, 2018, 8:30pm UTC](https://discourse.julialang.org/t/how-to-change-date-format-and-keep-the-type-of-variable/12194 "2018-07-05T20:30:21Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mvcavalcanti](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mvcavalcanti](https://discourse.julialang.org/u/mvcavalcanti)
#### Post date: [July 5, 2018, 8:30pm UTC](https://discourse.julialang.org/t/how-to-change-date-format-and-keep-the-type-of-variable/12194/1 "2018-07-05T20:30:21Z")

</div>

Hi.

I have an array of dates that I want to use on a xaxis of a plot. These dates I need to present as an abbreviated month/ abbreviated year way (For example: “Jan-18”, “Feb-18”, and so on). The problem shows up when I use Dates.format function. This converts my array of type Date to type string. So, what happens in my x axis plot is an alphabetically ordering, what is incorrect.

I think that if I could keep my array type, my x axis would order the dates in a correct way. How could I convert data format but keep the type? Is there a way to do that?

Thanks.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 5, 2018, 8:37pm UTC](https://discourse.julialang.org/t/how-to-change-date-format-and-keep-the-type-of-variable/12194/2 "2018-07-05T20:37:34Z")

</div>

this may not be the most general solution, but you can have your x-tick labels be a tuple of vectors.

```julia
xticks = ([1,2,3], String.([1,2,3]))

```

---

<div class="post-metadata">

### Author: ![mvcavalcanti](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mvcavalcanti](https://discourse.julialang.org/u/mvcavalcanti)
#### Post date: [July 5, 2018, 9:31pm UTC](https://discourse.julialang.org/t/how-to-change-date-format-and-keep-the-type-of-variable/12194/3 "2018-07-05T21:31:26Z")

</div>

I’m sorry, I didn’t understand… Actually, this piece of code didn’t work here. How could I make a parallel with my case?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 5, 2018, 10:01pm UTC](https://discourse.julialang.org/t/how-to-change-date-format-and-keep-the-type-of-variable/12194/4 "2018-07-05T22:01:09Z")

</div>

We need an MWE to give you exact code, but here is some code I made the other day that works

```julia
 xticks = ([30, 50, 105], ["2-year endline", "4-year endline", "9-year endline"])

```

I would imagine that you have a vector of dates for dates, so you could write

```julia
xticks = (mydates, Dates.format.(mydates))

```

---

<div class="post-metadata">

### Author: ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)
#### Post date: [July 6, 2018, 2:07am UTC](https://discourse.julialang.org/t/how-to-change-date-format-and-keep-the-type-of-variable/12194/5 "2018-07-06T02:07:31Z")

</div>

Here is a generic way of doing this in `Gadfly`:

```julia
using Base.Dates, DataFrames, Gadfly
dates = Date("2000-01-15"):Month(1):Date("2017-12-15")
dict1 = Dict(Dates.value(x) => Dates.format(x, "u-yy") for x in dates)

t = Dates.value.(dates)
D = DataFrame(t=t, date=dates, y= -cos.(2π*t/365.25))

pa = plot(D, x=:t, y=:y, Geom.line,
        Guide.xticks(ticks=t[1:6:end]), # location of tickmarks
        Scale.x_continuous(labels=i->dict1[i])
    )
draw(PNG(8inch,3inch), pa)

```

 ![dates_a](https://global.discourse-cdn.com/julialang/original/3X/0/0/00c551e052f0cff0197c08683e6ae113fa84ecc3.png)  
Note that if you omit `ticks` and `labels`, then `Gadfly` will try to pick suitable values, e.g.

```julia
pb = plot(D[1:24,:], x=:date, y=:y, Geom.line,
    Guide.xticks(orientation=:vertical)
)
draw(PNG(8inch,3inch),pb)

```

 ![dates_b](https://global.discourse-cdn.com/julialang/original/3X/4/3/43b56e5495c1200954da7603ae5a6a0b4bb0a190.png)

---

<div class="post-metadata">

### Author: ![mvcavalcanti](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mvcavalcanti](https://discourse.julialang.org/u/mvcavalcanti)
#### Post date: [July 7, 2018, 10:13pm UTC](https://discourse.julialang.org/t/how-to-change-date-format-and-keep-the-type-of-variable/12194/6 "2018-07-07T22:13:50Z")

</div>

@pdeffebach Thank you, I’m sure that your explanation will be very useful for me.

@Mattriks Thank you very much, your example was very helpful. It seems that you used a specific argument of the Gadfly pkg to change the xaxis label, but for my application, I really need an array of dates with a personalized format but still of type date, if it were possible, sure. Something like Excel, where we change a number format to a month/year, for example, but although we see a “string”, we still have a number. I’m sure that if it was possible, I could correctly order my x-axis plot.

Another option, if it were possible, could be change the levels of my array of dates (as strings) and impose a “monthly” ordering. Is that possible one of these two options in Julia?

---

<div class="post-metadata">

### Author: ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)
#### Post date: [July 8, 2018, 2:31am UTC](https://discourse.julialang.org/t/how-to-change-date-format-and-keep-the-type-of-variable/12194/7 "2018-07-08T02:31:43Z")

</div>

I think the issue here is that a julia `Date` object doesn’t store a string representation of itself e.g. `dump(Date("2018-01-01"))`, the string representation only exists when you format/print it in some way (some might say it’s quantum!). See the docs for more info: [https://docs.julialang.org/en/stable/manual/dates/](https://docs.julialang.org/en/stable/manual/dates/).
