Date Functions

Updated · 8 min read
mediumPostgreSQL 18sqldate-functionsextractintervalto-chardate-trunc

Date functions pull dates apart, move them, measure between them and format them for people. "Payments by month", "due 30 days from enrollment", "how long has Srihitha been a member?" and "December 19, 2025" on a receipt all need them. This lesson covers the current date and time, EXTRACT, intervals, age, date_trunc and to_char, with real Fee Tracker results, and finishes with the chapter's checkpoint: a payments report that labels, formats and fills gaps all at once.

In One Minute

  • current_date and now() give today's date and the current moment.
  • EXTRACT(MONTH FROM paid_on) pulls out one part: year, month, day, day of week, quarter.
  • date + INTERVAL '1 month' moves by calendar units; date + 30 moves by days.
  • age(later, earlier) gives years, months and days; date_trunc('month', d) rounds down to the 1st.
  • to_char(d, 'MM/DD/YYYY') formats for display, as text.

The Current Date and Time

FunctionReturnsType
current_dateToday's dateDATE
now(), current_timestampThe current momentTIMESTAMPTZ
localtimestampCurrent date and time, no zoneTIMESTAMP

now() is the moment the current transaction started, so every row in one statement gets the same value. Their results change every run, so the examples in this course use the fixed date DATE '2026-01-05' as "today" instead; in your own queries, write current_date.

EXTRACT: One Part of a Date

SQL
EXTRACT(part FROM date_or_timestamp)
SQL▶ Try it
SELECT payment_id, paid_on,
       EXTRACT(YEAR FROM paid_on)  AS year,
       EXTRACT(MONTH FROM paid_on) AS month,
       EXTRACT(DOW FROM paid_on)   AS day_of_week
FROM payments
ORDER BY payment_id;
Output
 payment_id |  paid_on   | year | month | day_of_week
------------+------------+------+-------+-------------
          1 | 2025-08-28 | 2025 |     8 |           4
          2 | 2025-10-30 | 2025 |    10 |           4
          3 | 2025-12-03 | 2025 |    12 |           3
          4 | 2025-12-29 | 2025 |    12 |           1
          5 | 2026-01-05 | 2026 |     1 |           1
          6 | 2025-11-14 | 2025 |    11 |           5
          7 | 2025-12-19 | 2025 |    12 |           5
(7 rows)

DOW numbers the days from Sunday = 0, so 4 is Thursday and 1 is Monday. Other parts include DAY, QUARTER, WEEK, HOUR and MINUTE. EXTRACT is standard SQL; MySQL also offers YEAR(d) and MONTH(d), and SQL Server DATEPART(month, d).

2025-12-19 split into year 2025, month 12, day 19, day of week 5 (Friday) and quarter 4

Moving Dates: Intervals

An INTERVAL is a length of time in calendar units, such as '1 month', '2 weeks' or '1 year':

SQL▶ Try it
SELECT DATE '2026-01-05' + 30                  AS plus_30_days,
       DATE '2026-01-05' + INTERVAL '1 month'  AS plus_one_month,
       DATE '2026-01-31' + INTERVAL '1 month'  AS jan_31_plus_month,
       DATE '2026-01-05' - INTERVAL '1 year'   AS one_year_ago;
Output
 plus_30_days |   plus_one_month    |  jan_31_plus_month  |    one_year_ago
--------------+---------------------+---------------------+---------------------
 2026-02-04   | 2026-02-05 00:00:00 | 2026-02-28 00:00:00 | 2025-01-05 00:00:00
(1 row)

Two things to notice. Adding an interval to a date gives a timestamp (midnight on that day); cast with ::date to get a date back. And "one month after January 31" is February 28, because February has no 31st: months aren't a fixed number of days. A rolling window, "the last 30 days", is written paid_on >= DATE '2026-01-05' - 30.

January 5 plus 30 days is February 4; plus 1 month is February 5; January 31 plus 1 month is February 28

Measuring Time Between Dates

age(later, earlier) returns the gap in years, months and days, the way people say it:

SQL▶ Try it
SELECT first_name, joined_on,
       age(DATE '2026-01-05', joined_on) AS member_for
FROM students
ORDER BY student_id;
Output
 first_name | joined_on  |      member_for
------------+------------+-----------------------
 Srihitha   | 2024-08-19 | 1 year 4 mons 17 days
 Advitha    | 2025-01-13 | 11 mons 23 days
 Navika     | 2025-09-02 | 4 mons 3 days
(3 rows)

Srihitha has been a member for 1 year, 4 months and 17 days. For a plain count of days, subtract the dates instead, as Dates and Times showed. age is PostgreSQL only; MySQL has TIMESTAMPDIFF(MONTH, a, b) and SQL Server DATEDIFF(month, a, b).

Rounding Down to a Month

date_trunc('month', d) rounds a date down to the first moment of its month, which makes it the key to "per month" reports:

SQL▶ Try it
SELECT date_trunc('month', DATE '2025-12-19')::date AS month_start,
       (date_trunc('month', DATE '2025-12-19') + INTERVAL '1 month - 1 day')::date AS month_end;
Output
 month_start | month_end
-------------+------------
 2025-12-01  | 2025-12-31
(1 row)

The month's last day is "start of month, plus one month, minus one day", which handles 28-, 30- and 31-day months alike. GROUP BY date_trunc('month', paid_on) gives one row per month, as GROUP BY showed. date_trunc also accepts 'year', 'quarter', 'week' and 'day'.

Formatting Dates for People

to_char(date, format) turns a date into display text (PostgreSQL and Oracle):

SQL▶ Try it
SELECT to_char(DATE '2025-12-19', 'MM/DD/YYYY')          AS us_style,
       to_char(DATE '2025-12-19', 'FMMonth FMDD, YYYY')  AS written_out,
       to_char(DATE '2025-12-19', 'Dy, Mon DD')          AS short_form,
       to_char(TIMESTAMP '2026-01-05 18:05', 'FMHH12:MI AM') AS clock_time;
Output
  us_style  |    written_out    | short_form  | clock_time
------------+-------------------+-------------+------------
 12/19/2025 | December 19, 2025 | Fri, Dec 19 | 6:05 PM
(1 row)
CodeMeansExample
YYYY, MM, DDYear, month, day as numbers2025, 12, 19
Month, MonMonth name, full or shortDecember, Dec
Day, DyWeekday name, full or shortFriday, Fri
HH12, MI, AM12-hour clock, minutes, AM/PM6, 05, PM
FMFill mode: no padding spaces

to_date('12/19/2025', 'MM/DD/YYYY') goes the other way, reading text in a known format. MySQL uses DATE_FORMAT(d, '%m/%d/%Y') and SQL Server FORMAT(d, 'MM/dd/yyyy').

2025-12-19 as 12/19/2025, December 19, 2025 and Fri, Dec 19

Common Mistakes

EXTRACT(MONTH ...) for monthly totals across years. Different years' Decembers merge. Use date_trunc.

Forgetting that date plus interval is a timestamp. Cast back with ::date.

Sorting formatted dates. to_char output is text. Sort by the date column.

Using now() in examples or tests. Results change daily; use a fixed date for anything you compare.

Interview Corner

How do you get the month from a date in standard SQL?

EXTRACT(MONTH FROM date_column). MySQL also has MONTH(), and SQL Server DATEPART(month, ...).

How do you group rows by month in PostgreSQL?

GROUP BY date_trunc('month', date_column), which keeps the year, unlike grouping by the month number alone.

What does DATE '2026-01-31' + INTERVAL '1 month' return?

2026-02-28 (as a timestamp at midnight), because February has no 31st day.

Practice

Challenge (chapter checkpoint): Build a payments report: for each payment, show payment_id, paid_on, a timing label of 'On time' or 'Late' (compare paid_on with due_on), the month name of paid_on (such as December), and the method, with 'Unknown' for missing ones. Sort by payment_id.

Show solution
SQL▶ Try it
SELECT payment_id, paid_on,
       CASE WHEN paid_on <= due_on THEN 'On time' ELSE 'Late' END AS timing,
       to_char(paid_on, 'FMMonth') AS month_name,
       coalesce(method, 'Unknown') AS method
FROM payments
ORDER BY payment_id;
Output
 payment_id |  paid_on   | timing  | month_name | method
------------+------------+---------+------------+---------
          1 | 2025-08-28 | On time | August     | card
          2 | 2025-10-30 | On time | October    | card
          3 | 2025-12-03 | Late    | December   | check
          4 | 2025-12-29 | On time | December   | card
          5 | 2026-01-05 | On time | January    | Unknown
          6 | 2025-11-14 | On time | November   | card
          7 | 2025-12-19 | Late    | December   | check
(7 rows)

One query, three ideas from this chapter: CASE for the label, to_char for the month name (FM removes the padding spaces), and coalesce for the missing method. Payments 3 and 7 were late, and payment 5's method shows as Unknown.

Quick check: Date functions

3 questions
1What does EXTRACT(DOW FROM DATE '2025-12-19') return, if Sunday is 0?
2Why group monthly totals by date_trunc('month', paid_on) instead of EXTRACT(MONTH FROM paid_on)?
3What does to_char(DATE '2025-12-19', 'MM/DD/YYYY') return?

Key Takeaways

You want toWrite
The month numberEXTRACT(MONTH FROM paid_on)
Group by monthGROUP BY date_trunc('month', paid_on)
One month laterpaid_on + INTERVAL '1 month'
Time as a memberage(DATE '2026-01-05', joined_on)
US date displayto_char(paid_on, 'MM/DD/YYYY')

Was this tutorial helpful?

Lesson 54 of 115