Skip to content

Commit 0f22c9b

Browse files
authored
SQLite 3.42.0 milliseconds solution
Refs https://simonwillison.net/2023/May/18/sqlite/
1 parent eac8c1d commit 0f22c9b

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

sqlite/unix-timestamp-milliseconds-sqlite.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Fetching seconds since the epoch is easy:
66
```sql
77
select strftime('%s', 'now');
88
```
9-
Milliseconds is *much more complex*. After some digging around, I found the following recipe:
9+
Prior to [SQLite 3.42.0](https://sqlite.org/releaselog/3_42_0.html) (released in May 2023) milliseconds were *much more complex*.
10+
11+
After some digging around, I found the following recipe:
1012
```sql
1113
select cast(
1214
(julianday('now') - 2440587.5)
@@ -15,6 +17,12 @@ select cast(
1517
```
1618
[Try these both here](https://latest.datasette.io/_memory?sql=select%0D%0A++strftime%28%27%25s%27%2C+%27now%27%29+as+seconds_since_epoch%2C%0D%0A++cast%28%28julianday%28%27now%27%29+-+2440587.5%29+*+86400+*+1000+as+integer%29+as+ms_since_epoch%3B).
1719

20+
In SQLite 3.42.0 and higher you can do this instead:
21+
22+
```sql
23+
select unixepoch('subsec');
24+
```
25+
1826
## Displaying them as human readable strings
1927

2028
This fragment of SQL turns them back into a readable UTC value:

0 commit comments

Comments
 (0)