Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert milliseconds to date in SQLite

Tags:

I store date from Calendar.getTimeInMilliseconds() in SQLite DB. I need to mark first rows by every month in SELECT statement, so I need convert time in milliseconds into any date format using SQLite function only. How can I avoid this?

like image 463
Konstantin.Efimenko Avatar asked Jun 07 '13 05:06

Konstantin.Efimenko


2 Answers

One of SQLite's supported date/time formats is Unix timestamps, i.e., seconds since 1970. To convert milliseconds to that, just divide by 1000.

Then use some date/time function to get the year and the month:

SELECT strftime('%Y-%m', MillisField / 1000, 'unixepoch') FROM MyTable 
like image 60
CL. Avatar answered Sep 20 '22 09:09

CL.


Datetime expects epochtime, which is in number of seconds while you are passing in milliseconds. Convert to seconds & apply.

SELECT datetime(1346142933585/1000, 'unixepoch'); 

Can verify this from this fiddle

http://sqlfiddle.com/#!5/d41d8/223

like image 27
Mit Bhatt Avatar answered Sep 19 '22 09:09

Mit Bhatt