48

I am storing a timestamp field in a SQLite3 column as TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP and I was wondering if there was any way for it to include milliseconds in the timestamp as well?

2
  • TIMESTAMP is number of seconds that passed since 1st Jan 1970 so guess you will need another field for miliseconds
    – fsw
    Commented Jul 10, 2013 at 15:35
  • POSIX time is defined as elapsed milliseconds since midnight 01-Jan-1970 UTC. I would say that using milliseconds as a timestamp is ok as long as you're sure it's POSIX time (i.e. in UTC). Otherwise, the use of milliseconds for local timestamps is very unusual and you should use other numeric temporal formats instead. See: stackoverflow.com/questions/32670064/…
    – scottb
    Commented Sep 25, 2015 at 19:47

6 Answers 6

61

Instead of CURRENT_TIMESTAMP, use (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')) so that your column definition become:

TIMESTAMP DATETIME DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW'))

For example:

CREATE TABLE IF NOT EXISTS event
(when_ts DATETIME DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')));
3
  • With Oracle background the %f looks weird by itself, it's the same as Oracle's TO_DATE(... SS.FFF')
    – TWiStErRob
    Commented Aug 11, 2014 at 23:02
  • ...%H:%M:%f that's weird, why isn't it ...%H:%M:%S.%f for seconds, and milliseconds? the f indicates seconds is a double truncated to 6 decimal places, rather than an int? ok
    – MetaStack
    Commented Dec 16, 2022 at 16:43
  • From sqlite.org/lang_datefunc.html, %f fractional seconds: SS.SSS Commented Dec 20, 2022 at 10:57
31

To get number of milliseconds since epoch you can use julianday() with some additional calculations:

-- Julian time to Epoch MS     
SELECT CAST((julianday('now') - 2440587.5)*86400000 AS INTEGER); 
1
  • 2
    Add ROUND to get more accurate thousandths: CAST(ROUND((julianday('now') - 2440587.5)*86400000) As INTEGER)
    – celoftis
    Commented Oct 12, 2018 at 20:11
14

The following method doesn't require any multiplies or divides and should always produce the correct result, as multiple calls to get 'now' in a single query should always return the same result:

SELECT strftime('%s','now') || substr(strftime('%f','now'),4);

The generates the number of seconds and concatenates it to the milliseconds part from the current second+millisecond.

2
  • 1
    Can it produce a wrong result when the two strftime calls happen with a tiny delay and belong to different seconds?
    – algrid
    Commented Apr 20, 2022 at 16:29
  • 1
    @algrid A query is declarative, so the actual way the query is done is not specified; so in practice (in SQLite) it's not actually making multiple calls at different times.
    – Michael
    Commented Apr 20, 2022 at 18:04
12

Here's a query that will generate a timestamp as a string with milliseconds:

 select strftime("%Y-%m-%d %H:%M:%f", "now"); 

If you're really bent on using a numeric representation, you could use:

select julianday("now"); 
1
  • 3
    fyi, julianday() is not natively comparable with anything based on epoch time, whether seconds or milliseconds. You'll have to convert everywhere in your code.
    – Ed J
    Commented May 13, 2018 at 19:16
5

The accepted answer only gives you UTC. If you need a local time instead of UTC, use this:

strftime('%Y-%m-%d %H:%M:%f', 'now', 'localtime')
0

Another epoch ms method for sqlite > 3.42.0 which added subsecond

SELECT unixepoch('subsecond') * 1000;
1703231951563.0

The value is a real, but in an integer column the value will be cast.

CREATE TABLE "items" (
  "created_at" integer default(unixepoch('subsecond') * 1000) not null
);

As in cast(x as int)

SELECT cast(unixepoch('subsecond') * 1000 as int);
1703231955581
3
  • the provided snippets return NULL Commented Jul 6 at 16:03
  • That's not a useful failure mode for older versions.
    – Matt
    Commented Jul 16 at 2:39
  • I do not understand your comment Commented Aug 17 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.