4

In an sqlite3 database, I am storing date and time as unix timestamps. Examples of unix timestamp we can find on our database: 1457600307000, 1457600109000, 1457599991000.

When executing the following query:

SELECT datetime(my_date, 'unixepoch') as last_update 
FROM my_table;

the results are not what we expect. the date we are getting from executing that query is: -1413-03-01 13:07:12

when we would actually expect a date and time from March / April 2016.

Is there anyway I can sqlite3 to read the timestamps correctly?

1 Answer 1

5

There is difference between number of seconds from start of epoch and number of miliseconds. Divide by 1000:

SELECT my_date, datetime(my_date/1000, 'unixepoch') as last_update 
FROM my_table;

SqlFiddleDemo

Output:

╔════════════════╦═════════════════════╗
║    my_date     ║     last_update     ║
╠════════════════╬═════════════════════╣
║ 1457600307000  ║ 2016-03-10 08:58:27 ║
║ 1457600109000  ║ 2016-03-10 08:55:09 ║
║ 1457599991000  ║ 2016-03-10 08:53:11 ║
╚════════════════╩═════════════════════╝

when we would actually expect a date and time from March / April 2016.

It looks like it is in range.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.