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
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
Disclaimer: not a guru
Dates.format(now(), "e dd u yyyy I:MMp")
# output:
"Sun 02 Feb 2025 1:19AM"
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> 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"
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> Using Dates
I gaps, ask for help, and after scrolling (I’m in a terminal…) I see
help?> Date
[... snip ...]
Examples
≡≡≡≡≡≡≡≡
julia> Date("2020-01-01", "yyyy-mm-dd")
2020-01-01
[... snip ...]
so I try:
Date(now(),"yyyy-mm-dd")
which gives an error:
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.
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.
The snippet you omitted reads
Construct a
Date
by parsing thed
date string following the pattern given in theformat
string
now()
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.
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:
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
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…