How to Format a Date in PostgreSQL
Last Updated :
08 Oct, 2024
PostgreSQL provides powerful tools to format dates into different styles and patterns using functions like TO_CHAR()
. Formatting dates in PostgreSQL is important when we need to present data in a more readable or application-friendly way.
This article will explain how to format a date in PostgreSQL and cover various formatting options.
How to Format a Date in PostgreSQL?
In PostgreSQL, dates are stored in a standard format (e.g., YYYY-MM-DD
). In many scenarios, we might need to display these dates in different formats such as MM-DD-YYYY
, Month DD, YYYY
, or even custom formats.
The most commonly used function for formatting dates in PostgreSQL is TO_CHAR().
which converts a timestamp
, date
, or time
value to a string according to a specified format.
Formatting Dates Using TO_CHAR()
The TO_CHAR()
function in PostgreSQL allows us to convert a date or timestamp to a string with a specific format.
Syntax:
TO_CHAR(date_value, 'format_pattern')
Key terms:
date_value
: The date or timestamp we want to format.
format_pattern
: A string that defines the format for the output.
Common Date Format Patterns
YYYY
: Four-digit yearYY
: Last two digits of the yearMM
: Month as a two-digit number (01-12)Mon
: Abbreviated month name (e.g., Jan, Feb)Month
: Full month name (e.g., January, February)DD
: Day of the month (01-31)Day
: Full day name (e.g., Monday, Tuesday)DY
: Abbreviated day name (e.g., Mon, Tue)HH24
: Hour (24-hour format)MI
: MinutesSS
: Seconds
1. How to Get a Date in the “DD/MM/YYYY” Format?
In PostgreSQL, we can format dates using the TO_CHAR()
function. To display the current date in the “DD/MM/YYYY” format, we can specify the appropriate format string.
Query:
SELECT TO_CHAR(NOW(), 'DD-MM-YYYY') AS formatted_date;
Output:
Explanation:
This query formats the current date (NOW()
) into the DD-MM-YYYY
format. For example, if today's date is September 25, 2024, the output will be 25-09-2024
.
2. How to Get a Date in the “MON DD, YY” Format?
To display the current date in a more descriptive format, we can use the TO_CHAR
function. By specifying the format as '
MON DD, YYYY
'
, the date will be presented with the full month name, the day of the month, and the year, making it easy to read and understand
Query:
SELECT TO_CHAR(NOW(), 'MON DD, YYYY') AS formatted_date;
Output:
formatted_date |
---|
September 25, 2024 |
Explanation:
- The query uses the
TO_CHAR
function to format the current date to display the full month name, the day of the month, and the four-digit year.
- The resulting output provides a clear and readable date format, such as "September 25, 2024."
3. Formatting Date to MM/DD/YY
To format the current date in the MM/DD/YY format, we can use the TO_CHAR
function. This format is commonly used in the United States and displays the month, day, and two-digit year.
Query:
SELECT TO_CHAR(NOW(), 'MM/DD/YY') AS formatted_date;
Output:
Explanation:
- The query uses
TO_CHAR
function to format the current date into a string that follows the MM/DD/YY format.
- The resulting output provides a clear and concise date format, such as "09/25/24."
4. Formatting Date with Day Name and Time
To display the current date along with the day of the week and the time, we can use the TO_CHAR
function with a specific format string. This format includes the full day name, the day of the month, the full month name, the year, and the time in a 24-hour format.
Query:
SELECT TO_CHAR(NOW(), 'Day, DD Month YYYY HH24:MI:SS') AS formatted_date;
Output:
formatted_date |
---|
Wednesday, 25 September 2024 14:05:32 |
Explanation:
This query includes the full day name, day of the month, full month name, year, hour, minute, and second in the formatted output.
5. Formatting Date to Display Quarter and Year
To display the current quarter of the year along with the year itself, we can use the TO_CHAR
function with the appropriate format string. The Q
format specifier is utilized to retrieve the quarter number.
Query:
SELECT TO_CHAR(NOW(), 'Q "Quarter of" YYYY') AS formatted_date;
Output:
formatted_date |
---|
3 Quarter of 2024 |
Explanation:
This query uses the Q
format specifier to display the quarter of the year, followed by the year. For example, 3 Quarter of 2024
if today's date falls in the third quarter of the year.
Conclusion
PostgreSQL offers various date formatting options using the TO_CHAR()
function, enabling us to display dates in different ways.
This flexibility is helpful for reporting, displaying data, or adjusting date formats to meet specific business needs. By understanding these formatting patterns, we can present dates exactly as needed for our purposes.
Similar Reads
How to Extract Date From a TimeStamp in PostgreSQL
PostgreSQL is a powerful open-source relational database management system (RDBMS). PostgreSQL is well-known for its feature-rich capabilities, standardization, and adaptability. It supports a variety of data types, complex SQL queries, and ACID properties. PostgreSQL offers scalability and durabili
4 min read
How to Use Date Formats in R
In this article, we will be looking at the approaches to using the date formats in the R programming language, R programming language provides several functions that deal with date and time. These functions are used to format and convert the date from one form to another form. R provides a format fu
2 min read
How to Format date using strftime() in Python ?
In this article, we will see how to format date using strftime() in Python. localtime() and gmtime() returns a tuple representing a time and this tuple is converted into a string as specified by the format argument using python time method strftime(). Syntax: time.strftime(format[, sec]) sec: This i
2 min read
How to Format Date in TypeScript ?
Formatting dates is important especially when displaying them to the users or working with date-related data. TypeScript provides various ways to achieve this. Below are the methods to format the date data type in TypeScript: Table of Content Using toLocaleString() methodUsing toLocaleDateString() m
4 min read
DATEADD() Function in PostgreSQL
PostgreSQL is a powerful, open-source relational database system known for its strength and wide range of functionalities. DATEADD function in PostgreSQL adds or subtract time intervals from a given date. In this article, we will discuss basic usage, advanced interval types, and practical examples f
6 min read
PostgreSQL - DATE_TRUNC Function
The PostgreSQL DATE_TRUNC() function is a powerful tool that helps us truncate a timestamp or interval to a specified level of precision. It is particularly useful when dealing with time-based data for effective data grouping and manipulation, making it ideal for reporting, analytics, and queries wh
4 min read
How to Extract Day of Week From Date Field in PostgreSQL?
In PostgreSQL, extracting the day of the week from a date field is a common and essential task when working with temporal data. Knowing the day of the week is crucial for various applications, including scheduling, reporting, and analytical purposes. PostgreSQL provides multiple methods to achieve t
5 min read
PostgreSQL Date Functions
PostgreSQL is widely recognized for its comprehensive support for date and time manipulations, making it an excellent choice for applications requiring precise time management and complex calculations. This article explores the core PostgreSQL date functions, covering how to retrieve the current dat
4 min read
How to Change Date Format in Excel
You know that Excel is one of the best spreadsheets you can work on. Excel is the first preference of many users to create an impressive data set. Have you tried filling in the Date on the sheet? Date with the data is a common practice done by users in Excel to get information timeline. To help orga
7 min read
How to Select Dates Between Two Dates in PostgreSQL?
When managing a PostgreSQL database, we may often encounter scenarios where we need to filter data based on date ranges. This task is crucial for generating reports, analyzing trends, or retrieving time-sensitive information. However, querying for data within a specific date range can be a challengi
4 min read