Solution 1 :
The sqlite timestamp here is 13 digits, but how can one know its specific date.
You simply drop the 3 digits (the milliseconds) you do do this by dividing by 1000 or by using substring(createTime,1,10)
but this year cannot be obtained
If you want the year then you can use the strftime
function with the %Y
format String. e.g.
strftime('%Y',createTime/1000,'unixepoch','localtime')
I want to get time like 2022-11-03 15:00:54
What you have works. However it is simpler to use
datetime(createTime/1000,'unixepoch','localtime')
The following demonstrates the above (and also includes part of the official documentation regarding the formatting arguments):-
DROP TABLE IF EXISTS warehouse_inout_log;
CREATE TABLE IF NOT EXISTS warehouse_inout_log (createTime INTEGER);
INSERT INTO warehouse_inout_log VALUES
(1667458854391),(1667458752768),('1667458752768')
;
/*
The strftime() routine returns the date formatted according to the format string specified as the first argument. The format string supports the most common substitutions found in the strftime() function from the standard C library plus two new substitutions, %f and %J. The following is a complete list of valid strftime() substitutions:
%d day of month: 00
%f fractional seconds: SS.SSS
%H hour: 00-24
%j day of year: 001-366
%J Julian day number (fractional)
%m month: 01-12
%M minute: 00-59
%s seconds since 1970-01-01
%S seconds: 00-59
%w day of week 0-6 with Sunday==0
%W week of year: 00-53
%Y year: 0000-9999
%% %
*/
SELECT
strftime('%Y',createTime/1000,'unixepoch','localtime') AS createYear,
datetime(createTime/1000,'unixepoch','localtime') AS createDateTime,
strftime('%Y',substr(createTime,1,10),'unixepoch','localtime') AS createYearV2,
datetime(substr(createTime,1,10),'unixepoch','localtime') AS createDateTimeV2
FROM warehouse_inout_log
;
DROP TABLE IF EXISTS warehouse_inout_log;
Resulting in:-
Problem :
The sqlite timestamp here is 13 digits, but how can one know its specific date. 1667458854391 should correspond to November 2022, but this year cannot be obtained regardless of interpretation or other conversions. I want to get time like 2022-11-03 15:00:54.
This method only works for ten digits
datetime(‘now’,’unixepoch’, ‘localtime’)
This is sqllite timestamp, but product specific model does not know,only know that this timestamp is 13-digit just like these: 1667458854391 1667458752768
I tried to do this but it didn’t feel compliant
datetime(substr(warehouse_inout_log.createTime,1,10),’unixepoch’, ‘localtime’),
Comments
Comment posted by blackapps
And if you remove the last three digits? Nano seconds. Milli seconds. Micro seconds. What is used? How many digits do you want? Did you expect?
Comment posted by blackapps
Tell us which timestamp should 2022-11-03 15:00:54 have if it was like you wanted. Show the code.