Hi everyone!
I was writing a little command line tool that involves parsing and formatting dates, and ran across something weird- format works fine with the dash 2019-10-10 as date separator, but it crashes using a period sign 2019.10.10. Why could this be? I used the ugly workaround below for now, but how is this supposed to work?
import times
let dt = now()
# works
echo dt.format("YYYY-mm-dd")
# '.' is not a valid pattern [TimeFormatParseError]
echo dt.format("YYYY.mm.dd")
# ugly workaround
echo dt.format("YYYY-mm-dd").replace('-', '.')
Thanks! Carlo
According to the documentation:
Other strings can be inserted by putting them in ''. For example hh'->'mm will give 01->56. The following characters can be inserted without quoting them: : - ( ) / [ ] ,. A literal ' can be specified with ''.
So if you wanna print like that, your format string should look like this: dt.format("YYYY'.'mm'.'dd"). Here's a working sample on the playground: https://play.nim-lang.org/#ix=2db8
That's because there's only a few characters, which can be used directly in a format string.
To use other characters / longer strings, you have to put them between ' ', like so:
echo dt.format("YYYY'.'mm'.'dd")
See below the table here:
https://nim-lang.github.io/Nim/times.html#parsing-and-formatting-dates