← Back to PostgreSQL Course | Chapter 5: Filtering & Functions | Lesson 3 of 7

Date के Functions

Date functions current time पढ़ते हैं, parts extract करते हैं, और intervals से arithmetic करते हैं।

In this page:

  1. Date Functions
Syntax
sql
SELECT now(), CURRENT_DATE;
SELECT EXTRACT(field FROM date_column) FROM table_name;
SELECT date_column + INTERVAL 'n days' FROM table_name;

Date Functions

now() और CURRENT_DATE current time और date return करते हैं। EXTRACT(field FROM date) year, month, dow, और epoch जैसे parts return करता है, date_trunc(month, ts) एक unit तक नीचे round करता है, age(a, b) एक interval देता है, और to_char dates को text की तरह format करता है।

INTERVAL values add करना dates shift करता है।

Note: date_trunc month से group करने का सबसे आसान तरीका है।

उदाहरण: Date functions

bash
shop=# SELECT EXTRACT(year FROM DATE '2024-03-15') AS y, EXTRACT(dow FROM DATE '2024-03-15') AS weekday;
  y   | weekday
------+---------
 2024 |       5
shop=# SELECT date_trunc('month', TIMESTAMP '2024-03-15 10:30:00') AS month_start;
     month_start
---------------------
 2024-03-01 00:00:00
shop=# SELECT age(DATE '2024-03-15', DATE '2000-01-01') AS age;
           age
-------------------------
 24 years 2 mons 14 days
shop=# SELECT DATE '2024-03-15' + INTERVAL '1 month' AS next_month;
      next_month
---------------------
 2024-04-15 00:00:00

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. comparison के लिए dates को text की तरह format करना
  2. now() के साथ time zones ignore करना
  3. date arithmetic के लिए string concatenation इस्तेमाल करना
चैप्टर सारांश
  • now() और CURRENT_DATE
  • EXTRACT parts पाता है
  • date_trunc नीचे round करता है
  • INTERVAL durations add करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.