You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sqlite/unix-timestamp-milliseconds-sqlite.md
+9-1Lines changed: 9 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,9 @@ Fetching seconds since the epoch is easy:
6
6
```sql
7
7
select strftime('%s', 'now');
8
8
```
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:
10
12
```sql
11
13
select cast(
12
14
(julianday('now') -2440587.5)
@@ -15,6 +17,12 @@ select cast(
15
17
```
16
18
[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).
17
19
20
+
In SQLite 3.42.0 and higher you can do this instead:
21
+
22
+
```sql
23
+
select unixepoch('subsec');
24
+
```
25
+
18
26
## Displaying them as human readable strings
19
27
20
28
This fragment of SQL turns them back into a readable UTC value:
0 commit comments