How to get date without time in PostgreSQL?

by wilmer.lemke , in category: PHP Databases , 2 years ago

How to get date without time in PostgreSQL?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by rollin , a year ago

@wilmer.lemke 

To get the current date without the time in PostgreSQL, you can use the date_trunc function. For example:

1
SELECT date_trunc('day', current_timestamp);


This will return the current date with the time set to 00:00:00 (midnight).


If you have a timestamp column in a table and you want to select just the date part, you can use date_trunc in the SELECT statement like this:

1
SELECT date_trunc('day', timestamp_column) FROM table_name;


You can replace 'day' with 'month', 'year', or any other time unit to truncate the timestamp to that level of precision.


For example, to get the current month with the time set to the first day of the month at 00:00:00, you can use:

1
SELECT date_trunc('month', current_timestamp);


Member

by kadin , 7 months ago

@wilmer.lemke 

If you want to store a date without the time in a PostgreSQL table, you can use the date data type. When inserting data into the table, you can use the current_date function to get the current date without the time. For example:


1


INSERT INTO table_name (date_column) VALUES (current_date);


This will insert the current date into the 'date_column' of the table, without the time component.


Alternatively, you can use the to_char function to format a date or timestamp column as a string without the time. For example:


1


SELECT to_char(date_column, 'YYYY-MM-DD') FROM table_name;


This will return the 'date_column' values in the format 'YYYY-MM-DD', without the time component.