Dates and times can be complicated. PostreSQL has a number of utilities that help store dates and times correctly and calculate time intervals or age.
```SQL
CREATE TABLE dates (
date DATE,
timestamp TIMESTAMP,
time_with_timezone TIMESTAMPTZ,
time TIME,
interval INTERVAL -- unique to Postgres
);
INSERT INTO dates (
date,
timestamp,
timestamp_with_timezone,
time,
interval
) VALUES (
DATE '2022-11-02',
'1999-01-08:04:05:06',
'1999-01-08:04:05:06',
'04:05:06')
'0000-00-07:00:00:00';
```
Postgres stores date/time values as [[UTC]]. If you run `SELECT NOW()` you will see the time is UTC, not local time.
## extract
Use `EXTRACT` to get a day, week, month, year, decade or century from a date.
```SQL
EXTRACT(DAY FROM NOW());
```
An alternative is `DATE_PART`.
```SQL
DATE_PART('year', my_date)
```
## time interval
See the [docs](https://www.postgresql.org/docs/current/datatype-datetime.html) for more detail.
```SQL
NOW() + INTERVAL '1 YEAR';
```
## age
```SQL
AGE(<birthday>)
AGE(birthday, NOW())::text
```
## to_char
`to_char` is a SQL function that extracts a date part from a dat as a character string.
```SQL
to_char(my_date, 'month')
```