MariaDB LOCALTIMESTAMP() Explained

In MariaDB, LOCALTIMESTAMP and LOCALTIMESTAMP() are synonyms for NOW().

The NOW() function is a built-in date and time function that returns the current date and time.

The time is returned in either 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is being used in a string or numeric context.

Syntax

LOCALTIMESTAMP() can be used in the following ways:

LOCALTIMESTAMP
LOCALTIMESTAMP([precision])

Where precision is the microsecond precision.

You can alternatively call NOW() like this:

NOW([precision])

Example

Here’s an example:

SELECT 
    LOCALTIMESTAMP,
    LOCALTIMESTAMP(),
    NOW();

Result:

+---------------------+---------------------+---------------------+
| LOCALTIMESTAMP      | LOCALTIMESTAMP()    | NOW()               |
+---------------------+---------------------+---------------------+
| 2021-05-10 09:11:33 | 2021-05-10 09:11:33 | 2021-05-10 09:11:33 |
+---------------------+---------------------+---------------------+

We can see that all three return the same result.

Numeric Context

When used in a numeric context, the resulting time is in YYYYMMDDHHMMSS.uuuuuu format.

Example:

SELECT 
    LOCALTIMESTAMP,
    LOCALTIMESTAMP + 0,
    LOCALTIMESTAMP() + 0;

Result:

+---------------------+--------------------+----------------------+
| LOCALTIMESTAMP      | LOCALTIMESTAMP + 0 | LOCALTIMESTAMP() + 0 |
+---------------------+--------------------+----------------------+
| 2021-05-10 09:11:49 |     20210510091149 |       20210510091149 |
+---------------------+--------------------+----------------------+

Precision

When used with the LOCALTIMESTAMP([precision]) syntax, you can specify the microsecond precision for the result.

Example:

SELECT 
    LOCALTIMESTAMP(6),
    LOCALTIMESTAMP(6) + 0;

Result:

+----------------------------+-----------------------+
| LOCALTIMESTAMP(6)          | LOCALTIMESTAMP(6) + 0 |
+----------------------------+-----------------------+
| 2021-05-10 09:12:09.119326 | 20210510091209.119326 |
+----------------------------+-----------------------+

The maximum value for the microsecond precision is 6. Here’s what happens when a higher number is passed for the precision:

SELECT LOCALTIMESTAMP(7);

Result:

ERROR 1426 (42000): Too big precision 7 specified for 'current_timestamp'. Maximum is 6

Adding to the Current Timestamp

There are many ways to perform arithmetic on datetime values in MariaDB.

Here’s an example of using the addition operator (+) to add 2 days to the date:

SELECT 
    LOCALTIMESTAMP,
    LOCALTIMESTAMP + INTERVAL 2 DAY;

Result:

+---------------------+---------------------------------+
| LOCALTIMESTAMP      | LOCALTIMESTAMP + INTERVAL 2 DAY |
+---------------------+---------------------------------+
| 2021-05-10 09:12:29 | 2021-05-12 09:12:29             |
+---------------------+---------------------------------+

Also see functions like DATE_ADD() and ADDDATE() for an alternative way to add to the current date.

Subtracting from the Current Date

Here’s an example of using the subtraction operator (-) to subtract 10 days from the current date:

SELECT 
    LOCALTIMESTAMP,
    LOCALTIMESTAMP - INTERVAL 10 DAY;

Result:

+---------------------+----------------------------------+
| LOCALTIMESTAMP      | LOCALTIMESTAMP - INTERVAL 10 DAY |
+---------------------+----------------------------------+
| 2021-05-10 09:12:45 | 2021-04-30 09:12:45              |
+---------------------+----------------------------------+

See functions like DATE_SUB() and SUBDATE() for an alternative way to subtract from the current date.