26

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

2
  • I want current format like MM/dd/yyyy like this. any idea how to get it ? Feb 15, 2015 at 10:42
  • See the linked documentation for formatting too. Feb 15, 2015 at 10:47

3 Answers 3

51

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'))
2
  • 1
    in order to get the time you have to use datetime('now') otherwise it will only return date.
    – Asha
    Sep 17, 2018 at 17:29
  • 1
    for MM/dd/yyyy use select strftime('%m/%d/%Y', 'now', 'localtime') Oct 26, 2018 at 20:24
4

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.

0

for your concrete case, this is what you need:

strftime('%m/%d/%Y',date('now'))

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.