# "simple" Dates one line question

**URL:** <https://discourse.julialang.org/t/simple-dates-one-line-question/125465>\
**Category:** General Usage\
**Tags:** dates, formatting\
**Created:** [February 1, 2025, 11:04pm UTC](https://discourse.julialang.org/t/simple-dates-one-line-question/125465 "2025-02-01T23:04:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Perrin\_Meyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/perrin_meyer/32/15686_2.png) [@Perrin\_Meyer](https://discourse.julialang.org/u/Perrin_Meyer)\
**Post date:** [February 1, 2025, 11:04pm UTC](https://discourse.julialang.org/t/simple-dates-one-line-question/125465/1 "2025-02-01T23:04:04Z")

</div>

can a Dates guru help me with the one line function `DateString(now())` that returns a string:

Sat 01 Feb 2025 2:33pm

I can’t figure out why

`Date(now()),DateFormat("yyyy-mm-dd"))` is not valid… but I’m also being lazy… thanks

---

<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:** [February 2, 2025, 12:17am UTC](https://discourse.julialang.org/t/simple-dates-one-line-question/125465/2 "2025-02-02T00:17:50Z")

</div>

Disclaimer: not a guru

```julia
Dates.format(now(), "e dd u yyyy I:MMp")

# output:
"Sun 02 Feb 2025 1:19AM"

```

---

<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 2, 2025, 2:13am UTC](https://discourse.julialang.org/t/simple-dates-one-line-question/125465/3 "2025-02-02T02:13:48Z")

</div>

Hi,

Rather than “being lazy” … give it a shot, show where you got,  
or let us know where you got stuck with the docs.

Anyway

```julia
julia> your_exemplar = "Sat 01 Feb 2025 2:33pm";
julia> your_test = DateTime("2025-02-01T14:33");
julia> your_format = "e dd u Y I:Mp";
julia> Dates.format(your_test, your_format)
Sat 01 Feb 2025 2:33PM"
julia> Dates.format(now(), your_format)
"Sat 01 Feb 2025 9:10PM"

```

---

<div class="post-metadata">

**Author:** ![Perrin\_Meyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/perrin_meyer/32/15686_2.png) [@Perrin\_Meyer](https://discourse.julialang.org/u/Perrin_Meyer)\
**Post date:** [February 2, 2025, 9:59pm UTC](https://discourse.julialang.org/t/simple-dates-one-line-question/125465/4 "2025-02-02T21:59:22Z")

</div>

I come from the Matlab and C DSP side of Julia, so all these fancy “dot” object things sometimes annoy me. On the command line, after

```julia
julia> Using Dates

```

I gaps, ask for help, and after scrolling (I’m in a terminal…) I see

```julia
help?> Date
[... snip ...]
 Examples
  ≡≡≡≡≡≡≡≡

  julia> Date("2020-01-01", "yyyy-mm-dd")
  2020-01-01
  [... snip ...]

```

so I try:

```julia
Date(now(),"yyyy-mm-dd")

```

which gives an error:

```julia
The type `Int64` exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
  Int64(::Float64)
   @ Base float.jl:990
[... snip ...] 

```

so clearly I did not understand the difference between Dates and Date and DateFormat and Dates.format (why plural?) so I punted to the list.

I thought in fancy-object-land functions like `now()` could know the right thing to return, or I’m completely misunderstanding everything (i.e. its not a complex float…). thanks.

---

<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:** [February 2, 2025, 10:14pm UTC](https://discourse.julialang.org/t/simple-dates-one-line-question/125465/5 "2025-02-02T22:14:28Z")

</div>

> [@Perrin\_Meyer](#):
>
> Dates.format (why plural?)

`Dates` is the name of the module in the standard library, and it defines a `format` method that is not exported perhaps to avoid polluting the namespace, so we need to access it using the module name first.

Btw, the Julia manual section on Dates is a [good source of info](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.format-Tuple%7BTimeType,%20AbstractString%7D).

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [February 2, 2025, 10:46pm UTC](https://discourse.julialang.org/t/simple-dates-one-line-question/125465/6 "2025-02-02T22:46:12Z")

</div>

> [@Perrin\_Meyer](#):
>
> I see
> 
> ```julia
> help?> Date
> [... snip ...]
> Examples
> ≡≡≡≡≡≡≡≡
> 
> julia> Date("2020-01-01", "yyyy-mm-dd")
> 2020-01-01
> [... snip ...]
> 
> ```
> 
> so I try:
> 
> `Date(now(),"yyyy-mm-dd")`

The [snippet you omitted](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.Date-Tuple%7BAbstractString,%20AbstractString%7D) reads

> Construct a `Date` by parsing the `d` date string following the pattern given in the `format` string

[`now()`](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.now-Tuple%7B%7D) is already a `Date`, which in case it wasn’t clear isn’t a string, there’s nothing to parse, this method has nothing to do with passing `now()` as “date” argument. Instead, you want to nicely format a `Date` object you already have.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [February 11, 2025, 4:06pm UTC](https://discourse.julialang.org/t/simple-dates-one-line-question/125465/7 "2025-02-11T16:06:24Z")

</div>

```julia
using Dates; # somewhere at the top of your file or module
Dates.format(now(), DateFormat("e dd u yyyy I:MMp"))

```

does almost what you want, but this actually more tricky than I though, what I came up with is:

```julia
n = now(); Dates.format(n, DateFormat("e d u yyyy I:MM ")) * lowercase(Dates.format(n, DateFormat("p")))

```

It’s my preference to use d, not dd, i.e. show 1 Jan, not 01 Jan, but the latter is better if you need to show in a table for alignment (you can use either when parsing). Your call. And you asked for pm, not PM and I found no other way to do this…

Note do NOT do redundant now(). They might not return same now, i.e. the first could do just before midnight and the other just after…

I see Java shows with lower case, as you asked for:

[https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/FormatStyle.html](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/FormatStyle.html)

p is case sensitive on input, and it may be intentional to be upper-case on output. It’s not wrong, PM and AM are acronyms after all…, it’s just pm and p.m. is rather commonly used… Your call if you like it spaced or not, maybe space isn’t preferred.

I can parse PM, spaced or not, but only either, not the both with the same format string, at least in Julia…, so consider what you do there. I use spaces since it’s not wrong, at least in some contexts (e.g. Wikipedia, then non-break space even better). It’s not too hard to output with the periods, actually often wanted, but I wouldn’t figure out how to parse such strings…

[Some other languages use a rather than p for the format string, and since it’s not used, maybe it could be for compatibility, and then be used to output in lower-case. It would be a slight breaking change, but who would want a a try a anyway on output…]

And with DateFormat rather than a naked string is slightly faster, e.g. in a loop. I like to say I’m neither an expert (on Julia), but maybe I’m getting there on this point… 🙂

Do use yyyy, not yyy, as I did at first incorrectly or yy. I was testing it out, and it will not be good for input that way, at least in Julia… I was trying to fix that, but my PR wasn’t accepted…
