Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite timestamp formatting

I am trying to work with dates in an sqlite database. I am storing my dates as timestamps, but when I use strftime() to format them to human readable dates I am getting back unxpected results.

Consider the following, I select the current timestamp:

SELECT strftime("%s","now"); 1281353727 

Then I try to format a date using the timestamp that I know to represent now expecting to get back a human readable format of todays date:

SELECT strftime('%d - %m  - %Y ', 1281353727); 01 - 04  - 3503 

Instead I get the above result. Is this correct behaviour? am I doing something wrong?

Thanks in advance,

Kevin

like image 823
Kevin Bradshaw Avatar asked Aug 09 '10 11:08

Kevin Bradshaw


People also ask

How do I change the date format in SQLite?

Use the STRFTIME() function to format date\time\datetime data in SQLite. This function takes two arguments. The first argument is a format string containing the date/time part pattern. In our example, we use the format string '%d/%m/%Y, %H:%M'.

What is the standard date and time format for a SQLite database?

The SQLite datetime function is a very powerful function that can calculate a date/time value, and return it in the format 'YYYY-MM-DD HH:MM:SS'.

How do I get the current date and time in SQLite?

The SQLite function DATE('now') returns the current date as a text value. It uses the 'YYYY-MM-DD' format, where YYYY is a 4-digit year, MM is a 2-digit month, and DD is a 2-digit day of the month. This function takes one argument: the text 'now' indicates the current date and time.

How does SQLite handle dates?

The SQLite date() function is used to calculate the date and return it in the format 'YYYY-MM-DD'. The SQLite datetime() function is used to calculate a date/time value, and return it in the format 'YYYY-MM-DD HH:MM:SS'. The SQLite julianday() function returns the date according to julian day.


1 Answers

You need to convert the timestamp to datetime first:

SELECT strftime('%d - %m  - %Y ', datetime(1281353727, 'unixepoch')) FROM Visits; 
like image 159
reko_t Avatar answered Sep 21 '22 14:09

reko_t