15

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:21:13-05 

From this I need

2000-12-16 12:21 (no seconds and milliseconds only date and time in hours and minutes)

From a timestamp with time zone field, say update_time, how do I get date as well as time like above using PostgreSQL select query.

Please help me.

informatik01
  • 16,038
  • 10
  • 74
  • 104
user2515189
  • 539
  • 3
  • 9
  • 19
  • 1
    Er ... `SELECT fieldname FROM table`. More details please, this doesn't make much sense as written. PostgreSQL version? Client program/driver you're using to access PostgreSQL? Code that shows the problem you're having? – Craig Ringer Jun 28 '13 at 10:53
  • try some of these: http://www.postgresql.org/docs/9.1/static/functions-datetime.html – davek Jun 28 '13 at 10:54

3 Answers3

23

There are plenty of date-time functions available with postgresql:

See the list here

http://www.postgresql.org/docs/9.1/static/functions-datetime.html

e.g.

SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 16

For formatting you can use these:

http://www.postgresql.org/docs/9.1/static/functions-formatting.html

e.g.

select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI') ...
davek
  • 22,499
  • 9
  • 75
  • 95
23

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());
Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
5

This should be enough:

select now()::date, now()::time
    , pg_typeof(now()), pg_typeof(now()::date), pg_typeof(now()::time)
albfan
  • 12,542
  • 4
  • 61
  • 80