← Back to PostgreSQL Course | Chapter 3: Data Types | Lesson 4 of 7

DATE और TIMESTAMP

PostgreSQL में date, time, timestamp, और timestamptz types हैं rich date arithmetic के साथ।

In this page:

  1. DATE/TIMESTAMP
Syntax
sql
column_name date
column_name timestamp
column_name timestamptz DEFAULT now()

DATE/TIMESTAMP

date एक calendar day store करता है, timestamp बिना zone के date और time store करता है, और timestamptz एक absolute moment (internally UTC) store करता है जो session time zone में display होता है।

Dates subtract करना days की एक integer संख्या देता है, और INTERVAL '7 days' जैसे intervals add किए जा सकते हैं। now() और CURRENT_DATE current time देते हैं।

Note: global users के लिए events के लिए timestamptz इस्तेमाल करें ताकि time zones सही से handle हों।

उदाहरण: DATE/TIMESTAMP

bash
shop=# SELECT DATE '2024-03-15' + 30 AS plus_30_days;
 plus_30_days
--------------
 2024-04-14
shop=# SELECT DATE '2024-03-15' - DATE '2024-03-01' AS days_between;
 days_between
--------------
           14
shop=# SELECT TIMESTAMP '2024-03-15 10:30:00' + INTERVAL '2 hours 15 minutes' AS later;
        later
---------------------
 2024-03-15 12:45:00
shop=# SELECT to_char(TIMESTAMPTZ '2024-03-15 10:30:00+00', 'YYYY-MM-DD HH24:MI') AS formatted;
    formatted
------------------
 2024-03-15 10:30

⚠️ 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. global users के लिए बिना time zones के timestamps store करना
  2. dates को text की तरह compare करना
  3. session time zone ignore करना
चैप्टर सारांश
  • date, time, timestamp, timestamptz types मिलते हैं
  • Dates subtract होकर days देती हैं
  • INTERVAL values add करें
  • Events के लिए timestamptz prefer करें
🔒

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.