Date, Time, Age
The NOW
function returns the current date and time, but can be scoped to retrieved the individual fields.
In addition, arithmetic operation can be applied to NOW
using the INTERVAL
function.
AGE
can be calculated from the date of birth (DOB).
Syntax | Description |
---|---|
SELECT NOW(); |
The current date and time. |
SELECT NOW()::DATE; |
The current date. |
SELECT NOW()::TIME; |
The current time. |
SELECT AGE(NOW(), < DOB >); |
The age based on date of birth. |
Date Arithmetic
Date arithmetic can be done using the following intervals:
YEAR
, YEARS
, MONTH
, MONTHS
, DAY
and DAY
.
Example | Description |
---|---|
SELECT NOW() - INTERVAL '1 YEAR'; |
The date and time one year ago. |
SELECT (NOW() + INTERVAL '10 MONTHS'); |
The date in ten months. |
Extract
The EXTRACT
function extracts parts from a date. NOW
is the date used in the examples.
Example | Description |
---|---|
SELECT EXTRACT(CENTURY FROM NOW()); |
The current century. |
SELECT EXTRACT(YEAR FROM NOW()); |
The current year. |
SELECT EXTRACT(MONTH FROM NOW()); |
The current month. |
SELECT EXTRACT(DAY FROM NOW()); |
The current day. |
SELECT EXTRACT(DOW FROM NOW()); |
The current day of week. |