Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current date time format in SQlite?

Tags:

I am using sqlite for local database in mobile and in my database. i want to know that

How to get current date format in SQLITE? I want to get date in the next format: MM/dd/yyyy

like image 816
suraj mahajan Avatar asked Feb 15 '15 10:02

suraj mahajan


People also ask

Does SQLite have date format?

The date() function returns the date as text in this format: YYYY-MM-DD. The time() function returns the time as text in this format: HH:MM:SS. The datetime() function returns the date and time as text in their same formats: YYYY-MM-DD HH:MM:SS.

What is the use of SQLite time () function?

SQLite TIME() extracts the time part of a time or datetime expression as string format. The time() function returns the time as HH:MM:SS.

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'.


2 Answers

To get the current date you can use:

SELECT date('now'); 

Note: This is NOT a server date, it's the same time you get if you query the date and time directly from your application because SQLITE runs in-process.

It's mostly useful for putting a current time into a table or for some simple calculations if your language's date processing is very poor.

To do the calculations see the SQLITE Documentation

See the docs for formatting too for example:

SELECT strftime('%Y-%m-%d %H:%M:%S', datetime('now')) 
like image 155
user3710044 Avatar answered Oct 26 '22 11:10

user3710044


According to the SQLite documentation as of this writing (3/30/2020), in the section titled "The DEFAULT clause", it is recommended that a constant value be used instead of a sub-query.

In my experimentation, I also ran into issues with the SQLite CREATE TABLE statement that was generated during model creation by EF Core 3.0 when using SELECT datetime('now'). The SQLite data provider complained of a syntax error when using a SELECT statement within the CREATE table statement. As such, I would recommend using the CURRENT_TIMESTAMP keyword.

like image 38
JLunda Avatar answered Oct 26 '22 13:10

JLunda