# Julian way to do strptime?

**URL:** https://discourse.julialang.org/t/julian-way-to-do-strptime/20492
**Category:** General Usage
**Tags:** dates
**Created:** [February 6, 2019, 5:53am UTC](https://discourse.julialang.org/t/julian-way-to-do-strptime/20492 "2019-02-06T05:53:49Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [February 6, 2019, 5:53am UTC](https://discourse.julialang.org/t/julian-way-to-do-strptime/20492/1 "2019-02-06T05:53:49Z")

</div>

I’m converting an R script that uses `strptime` to extact datetime data from strings. Is there a more Julian way of doing this than calling `Base.Libc.strptime`?

I’m not very familiar with either string or date handling in Julia.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [February 6, 2019, 6:24am UTC](https://discourse.julialang.org/t/julian-way-to-do-strptime/20492/2 "2019-02-06T06:24:33Z")

</div>

> [@Raf](#):
>
> strptime

there is, what format specifiers occur in those uses of striptime? (show me a few examples, it is easier to explain how to do it in Julia with a few specific uses).

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [February 6, 2019, 7:29am UTC](https://discourse.julialang.org/t/julian-way-to-do-strptime/20492/3 "2019-02-06T07:29:43Z")

</div>

Sure:

```julia
Base.Libc.strptime.("SMAP_L4_SM_gph_%Y%m%dT%H%M%S", filenames)

```

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [February 6, 2019, 8:29am UTC](https://discourse.julialang.org/t/julian-way-to-do-strptime/20492/4 "2019-02-06T08:29:23Z")

</div>

Are all of the strings are prefixed with “SMAP\_L4\_SM\_gph\_” ?

(would 2018-May-17 at 22:18:05 be given as “SMAP\_L4\_SM\_gph\_20180517T221805”)

---

<div class="post-metadata">

### Author: ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)
#### Post date: [February 6, 2019, 8:38am UTC](https://discourse.julialang.org/t/julian-way-to-do-strptime/20492/5 "2019-02-06T08:38:26Z")

</div>

```julia
using Dates

str = "SMAP_L4_SM_gph_20180517T221805"

# Define the format, `S` and `M` need to be escaped because they
# are format identifiers
df = dateformat"\S\MAP_L4_\S\M_gph_yyyymmddTHHMMSS"

DateTime(str, df)
# 2018-05-17T22:18:05

```

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [February 6, 2019, 8:40am UTC](https://discourse.julialang.org/t/julian-way-to-do-strptime/20492/6 "2019-02-06T08:40:51Z")

</div>

first, lets convert a string that only contains the date+time, given in that same pattern (yyyymmddTHHMMSS) where the “T” serves to separate the date part from the time part.

```julia
using Dates

myformat = dateformat"yyyymmddTHHMMSS"

str = "20180517T221805"

mydatetime = DateTime(str, muformat)

```

with that this happens

```julia
julia> mydatetime = DateTime(str, myformat)
2018-05-17T22:18:05

julia> Date(mydatetime), Time(mydatetime)
(2018-05-17, 22:18:05)

```

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [February 6, 2019, 8:49am UTC](https://discourse.julialang.org/t/julian-way-to-do-strptime/20492/7 "2019-02-06T08:49:53Z")

</div>

now lets suppose that all your date+time strings are prefixed by some (nonempty) character sequence that ends with ‘\_’ (the [last] underscore in the string comes before the first digit of the year). This way you can have a slightly more general converter.

```julia
using Dates

myformat = dateformat"yyyymmddTHHMMSS"

strip_prefixed_chars(filenamestr) = split(filenamestr, '_')[end]

```

with that we can do this

```julia
julia> str = "SMAP_L4_SM_gph_20180517T221805"
"SMAP_L4_SM_gph_20180517T221805"

julia> mydatetime = DateTime(strip_prefixed_chars(str), myformat)
2018-05-17T22:18:05

```

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [February 6, 2019, 8:50am UTC](https://discourse.julialang.org/t/julian-way-to-do-strptime/20492/8 "2019-02-06T08:50:40Z")

</div>

Ok dateformat string macro is the thing! so obvious I missed it.

Thanks!

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [October 9, 2021, 9:28pm UTC](https://discourse.julialang.org/t/julian-way-to-do-strptime/20492/9 "2021-10-09T21:28:46Z")

</div>

But what if the string has a day-of-year? I couldn’t find a format that allows parsing, _e.g._  
“2021059T185321”

(many satellite products use variations of this to name their files)
