now Function

SQLite “now” is not actually a fuction, but “now” is a timestring parameter that is used in various SQLite functions to fetch the current date and time.

Syntax:

There are three types of syntax for now function in SQLite:

date('now')   

Or

time('now')  

Or

strftime(format, 'now')  

i.e.   

strftime('%Y-%m-%d','now')  

strftime('%Y-%m-%d %H-%M','now')  

strftime('%Y-%m-%d %H-%M-%S','now') 

    The third syntax is used when expressing the current date/time using the strftime function. Here “format” can be anyone of the following:

    IndexformatExplanation
    1)%YYear as 4 digits (0000 to 9999)
    2)%WWeek of year (00 to 53)
    3)%wDay of week (0 to 6, where 0 is Sunday)
    4)%mMonth of year (01 to 12)
    5)%dDay of month (00 to 31)
    6)%HHour (00 to 24)
    7)%MMinute (00 to 25)
    8)%SSeconds (00 to 59)
    9)%sSeconds since 1970-01-01
    10)%fFractional seconds (SS.SSS)
    11)%jDay of year (001 to 366)
    12)%JJulian day as a numeric value

    Example: Retrieve current date:

    SELECT date('now');  
    
    SELECT strftime('%Y-%m-%d','now');  

      Output:

      SQLite Now function 1
      SQLite Now function 2

      Example: Retrieve current time:

      SELECT time('now');  (HH-MM-SS Format)  
      
      SELECT strftime('%H-%M-%S','now');   (HH-MM-SS Format)   
      
      SELECT strftime('%H-%M-%f','now');  (HH-MM-SS.SSS Format)   
      
      SELECT strftime('%H-%M','now');  (HH-MM Format)

      Output:

      SQLite Now function 3
      SQLite Now function 4
      SQLite Now function 5
      SQLite Now function 6

      Comments

      Leave a Reply

      Your email address will not be published. Required fields are marked *