How to get the date and time from timestamp in PostgreSQL select query?

前端 未结 3 1477
北海茫月
北海茫月 2020-12-17 18:08

How to get the date and time only up to minutes, not seconds, from timestamp in PostgreSQL. I need date as well as time.

For example:

2000-12-16 12:2         


        
3条回答
  •  粉色の甜心
    2020-12-17 19:06

    To get the date from a timestamp (or timestamptz) a simple cast is fastest:

    SELECT now()::date
    

    You get the date according to your local time zone either way.

    If you want text in a certain format, go with to_char() like @davek provided.

    If you want to truncate (round down) the value of a timestamp to a unit of time, use date_trunc():

    SELECT date_trunc('minute', now());
    

提交回复
热议问题