SQLite Date and Time Functions (2007)
1vuio0pswjnm7 | 61 points | 14day ago | www2.sqlite.org
stillpointlab|12day ago
The fact that they do not include the trailing 'Z' for UTC timestamps is a frustration for me. It caused a few hours of debugging since JavaScript date parsing assumes that dates that lack the trailing Z are in the client time zone. I had to add a hack to check if a UTC date did or did not have the trailing Z and append the Z if it was missing.
This is made worse when you have a lot of `createdAt` columns that get set to NOW. You have to deal with the missing Z in all places where it matters. And in general, it is pointless to use the `localtime` parameter since that is the server time, and for UI I want to display the time local for the user. So I want to deal exclusively in UTC on the server and do any time zone conversions on the client.
Worth noting that when I changed to PostgreSQL, its date function does add the Z which makes life easier in general. But it is an inconsistency to be aware of if you use both DBs.
ncruces|12day ago
Try one of these:
strftime('%Y-%m-%dT%H:%M:%SZ')
strftime('%Y-%m-%dT%H:%M:%fZ')
You can use this to convert whatever internal format you're using for presentation, in a SELECT statement. Like so (be sure to read up on 'auto', to see if it fits): strftime('%Y-%m-%dT%H:%M:%fZ', column, 'auto')
ray_v|11day ago
not exactly convenient, but it indeed does get the job done and is flexible enough to handle whatever you need ... just frustrating that it doesn't exactly follow the ISO spec. Leaving the timezone specifier off makes it ambiguous and should be assumed to be a local time - which it is not (unless you lived in a timezone that was always in UTC).
nikeee|12day ago
`current_timestamp` also returns something like `2025-06-15 19:50:50` while the docs state that it is ISO 8601. Except that this is not ISO 8601 due to the T missing in the middle. This has caused some headaches due to different formats of JS's `.toISOString()` and SQLite's `current_timestamp`. The datetime column is basically only for documentation. I wish they had some timestamptz type which rejects insertions containing invalid datetime formats.
chuckadams|12day ago
ISO8601 is a collection of different formats, and using a space instead of a ‘T’ is one of the allowed variations. I’m not sure anything implements the full spec perfectly.
lelandbatey|12day ago
That is not true. I refer you to a PDF copy of ISO 8601[1] (a 2016 working draft copy, but still representative). Within section "4.3.2 Complete Representations" it reads as follows:
> The character [T] shall be used as time designator to indicate the start of the representation of the time of day component in these expressions. The hyphen [-] and the colon [:] shall be used, in accordance with 4.4.4, as separators within the date and time of day expressions, respectively, when required.
> NOTE By mutual agreement of the partners in information interchange, the character [T] may be omitted in applications where there is no risk of confusing a date and time of day representation with others defined in this International Standard.
They then show examples which clearly show that they mean you can not-include the T, but you CANNOT substitute T for a space.
Unless I am incorrectly reading the document or unaware of a further section indicating the further allowance of substituting a T for a space, you cannot swap T for space according to the standard.
1 - https://www.loc.gov/standards/datetime/iso-tc154-wg5_n0038_i...
chuckadams|11day ago
Yep, I got it confused with RFC3339, which starts off with a precise spec, then lays out vague exceptions in the prose like using a space. All I want for xmas this year is a proper datetime specification, from anyone but ISO (who still doesn't seem to understand that software engineers are never going to pay for a spec).
nikeee|11day ago
You probably mean RFC3339, which overlaps with ISO 8601 and allows this. But the docs don't mention that RFC and only reference the ISO standard.
You can compare the formats here: https://ijmacd.github.io/rfc3339-iso8601/
user7266|12day ago
It is not allowed to use any other separator than 'T'. You might be thinking about RFC3339
chuckadams|11day ago
You're absolutely right, I confused it with RFC3339. ISO8601 suggests that the 'T' is optional, but only in the sense of leaving it out... and fails to provide any actual examples of doing so.
The amount of handwaving in ISO8601 could turn a windmill, though in this respect RFC3339 is not much better. The ABNF clearly shows "T" (case-insensitive) as the only separator, but immediately afterward gives us this:
> NOTE: ISO 8601 defines date and time separated by "T". Applications using this syntax may choose, for the sake of readability, to specify a full-date and full-time separated by (say) a space character.
Separated by (say) whatever the hell you want apparently. I'm getting bruises on my face from all the palming.
euroderf|10day ago
I use an underbar ("_"). This nicely separates visually a date-time into those two respective parts while keeping them lexically in a single token/string that does not get broken up at line ends.
tshaddox|11day ago
> JavaScript date parsing assumes that dates that lack the trailing Z are in the client time zone.
The situation is in fact even worse than this. The current (ECMA262) spec states:
> When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.
This is in fact a huge bug in the spec, which was initially unintentional but is now deliberately preserved for web compatibility reasons.
More info here:
https://maggiepint.com/2017/04/11/fixing-javascript-date-web...
stillpointlab|11day ago
That is good context, thanks! Once I found my bug I had a moment of not being sure who to blame. Honestly, I'm a bit surprised that the ISO8601 spec dictates that absent the Z the date-time ought to be interpreted as local. At the least, I expected there would at least be a way for me to say "trust me JS, this date is UTC so please parse it that way" - but the only way I could find to force that to happen was to manually add a "Z".
But the insanity of inconsistently choosing local/UTC based on the presence of time is genuinely painful. Dates and times are hard enough as it is, why would they do that? It gives me some amusement that this was one of the motivating use cases behind the Temporal spec.
tshaddox|10day ago
> I'm a bit surprised that the ISO8601 spec dictates that absent the Z the date-time ought to be interpreted as local
My understanding is that an ISO8601 timestamp with no offset simply means that no time zone is specified, like what the JS Temporal API calls a PlainDateTime. This is a reasonable concept, and to avoid footguns it should be impossible to implicitly convert such a timestamp to a timestamp with a time zone.
This concept isn’t representable as a JS Date, which will always have a time zone specified (always the local time zone).
tshaddox|10day ago
> At the least, I expected there would at least be a way for me to say "trust me JS, this date is UTC so please parse it that way" - but the only way I could find to force that to happen was to manually add a "Z".
FWIW, I always use parseISO from date-fns for this. I agree it’s bonkers that this is necessary!
noitpmeder|12day ago
Seems it should be trivial to extend/change the data type to add a Z. It's not like it's storing the ISO8601 string in the db itself, so it's just a presentation later that is giving you the string.
em500|12day ago
You don't actually know how they're stored. SQLite has a rather idiosyncratic approach to datetimes: it does not provide any datetime data types (the only SQLite data types are NULL, INTEGER, REAL, TEXT and BLOB). It's left entirely to the user how to store datetimes using these types. What SQLite does provide are functions (documented on the submitted page) that translate some datetime representations (stored using the one of the mentioned basic datatypes) to other formats. So you can choose to store your datetimes in unix-epoch INTEGER and use the translation functions to output ISO8601 TEXT when needed, or the other way around: there is no correct or even preferred way in SQLite.
chasil|12day ago
You have two choices.
UNIX epoch time is defined as the number of seconds since 1970/1/1 midnight. You can do those.
I think there is also Julian time, which incorporates the Gregorian skip. It is stored as a floating point number, the integer portion being the day, and the fractional part being the hour.