postgresql----时间类型
postgresql支持的时间类型如下图所示:

日期 date:
建议日期的输入格式为1997-01-01,虽然也支持19970101,1/1/1997,Jan-1-1997等多种格式。
时间戳 timestamp[(p)] with(without) time zone:
其实配置文件是可以设置时区的,且做上层业务时也不会在多个时区间切换,所以一般使用无时区的时间戳就可以满足需要了。
建议时间戳的输入格式为1997-01-01 00:00:00
时间 time[(p)] with(without) time zone:
同样无时区的时间也是可以满足需要的,只表示一天的时间点,不包含日期,可以有如下格式:
12:00:00,120000,12:00
8:00 AM,8:00 PM
时间间隔 interval [fields][(p)]
我还是比较喜欢明确的时间间隔表示方法,如:
1 year 2 months 3 days 4 hours 5 minutes 6 seconds
可以用单词复数,也可以不用
test=# select interval '1 year 2 months 3 days 4 hours 5 minutes 6 seconds';
interval
-------------------------------
1 year 2 mons 3 days 04:05:06
(1 row) test=# select interval '2 year 2 months 3 days 4 hours 5 minutes 6 seconds';
interval
--------------------------------
2 years 2 mons 3 days 04:05:06
(1 row) test=# select interval '2 year 2 month 3 day 4 hour 5 minute 6 second';
interval
--------------------------------
2 years 2 mons 3 days 04:05:06
(1 row)
单词太复杂?记不住?
test=# select interval '1 y 2 mon 2d 12:09:12';
interval
-------------------------------
1 year 2 mons 2 days 12:09:12
(1 row) test=# select interval '1 y 2 mon 2d 12h 09m 12s';
interval
-------------------------------
1 year 2 mons 2 days 12:09:12
(1 row)
几个特殊日期和时间

以下是一些时间运算的示例:
test=# select timestamp(1) without time zone '2016-07-08 12:00:00.234';
timestamp
-----------------------
2016-07-08 12:00:00.2
(1 row)
test=# select time(1) without time zone '2016-07-08 12:00:00.234';
time
------------
12:00:00.2
(1 row)
test=# select date'2016-07-08' - 7;
?column?
------------
2016-07-01
(1 row) test=# select date'2016-07-08' + 7;
?column?
------------
2016-07-15
(1 row) test=#
test=# select date'2016-07-08' + interval'1 day 2h';
?column?
---------------------
2016-07-09 02:00:00
(1 row) test=# select date'2016-07-08' + time'22:00';
?column?
---------------------
2016-07-08 22:00:00
(1 row) test=# select interval'1day' + interval'1h';
?column?
----------------
1 day 01:00:00
(1 row) test=# select timestamp'2016-07-08 22:00:00' + interval'2hour';
?column?
---------------------
2016-07-09 00:00:00
(1 row) test=# select timestamp'2016-07-08 22:00:00' - date'2016-07-08';
?column?
----------
22:00:00
(1 row) test=# select 10*interval'1h';
?column?
----------
10:00:00
(1 row)
与格林威治时间相互转换
test=# select timestamp without time zone 'epoch';
timestamp
---------------------
1970-01-01 00:00:00
(1 row) test=# select timestamp without time zone 'epoch' + 3600*interval '1 sec';
?column?
---------------------
1970-01-01 01:00:00
(1 row)
时间函数:
| 函数 | 返回类型 | 描述 |
示例 |
结果 |
| age(timestamp, timestamp) | interval | 计算两个时间戳的时间间隔 |
select age(timestamp '2001-04-10', timestamp '1957-06-13'); |
43 years 9 mons 27 days |
| age(timestamp) | interval | 计算current_date与入参时间戳的时间间隔 |
select age(timestamp '2016-07-07 12:00:00'); |
12:00:00 |
| clock_timestamp() | timestamp with time zone | 当前时间戳(语句执行时变化) | select clock_timestamp(); | 2016-07-08 15:14:04.197732-07 |
| current_date | date | 当前日期 | select current_date; | 2016-07-08 |
| current_time | time with time zone | 当前时间 | select current_time; | 15:15:56.394651-07 |
| current_timestamp | timestamp with time zone | 当前时间戳 | select current_timestamp; | 2016-07-08 15:16:50.485864-07 |
| date_part(text, timestamp) | double precision | 获取时间戳中的某个子域,其中text可以为year,month,day,hour,minute,second等 |
select date_part('year',timestamp'2016-07-08 12:05:06'), date_part('month',timestamp'2016-07-08 12:05:06'), date_part('day',timestamp'2016-07-08 12:05:06'), date_part('minute',timestamp'2016-07-08 12:05:06'), date_part('second',timestamp'2016-07-08 12:05:06'); |
2016 | 7 | 8 | 12 | 5 | 6 |
| date_part(text, interval) | double precision | 功能同上,只是第二个入参为时间间隔 | select date_part('hour',interval'1 day 13:00:12'); | 13 |
| date_trunc(text, timestamp) | timestamp |
将时间戳截断成指定的精度, 指定精度后面的子域用0补充 |
select date_trunc('hour', timestamp'2016-07-08 22:30:33'); |
2016-07-08 22:00:00 |
| date_trunc(text, interval) | interval | 功能同上,只是第二个入参为时间间隔 | select date_trunc('hour',interval'1 year 2 mon 3 day 22:30:33'); | 1 year 2 mons 3 days 22:00:00 |
extract(field from timestamp) |
double precision | 功能同date_part(text, timestamp) | select extract(hour from timestamp'2016-07-08 22:30:29'); | 22 |
extract(field from interval) |
double precision | 功能同date_part(text, interval) | select extract(hour from interval'1 day 13:00:12'); | 13 |
| isfinite(date) | boolean | 测试是否为有穷日期 | select isfinite(date'2016-07-08'),isfinite(date'infinity'); | t,f |
| isfinite(timestamp) | boolean | 测试是否为有穷时间戳 | select isfinite(timestamp'2016-07-08'); | t |
| isfinite(interval) | boolean | 测试是否为有穷时间间隔 | select isfinite(interval'1day 23:02:12'); | t |
| justify_days(interval) | interval | 按照每月30天调整时间间隔 | select justify_days(interval'1year 45days 23:00:00'); | 1 year 1 mon 15 days 23:00:00 |
| justify_hours(interval) | interval | 按照每天24小时调整时间间隔 | select justify_hours(interval'1year 45days 343hour'); | 1 year 59 days 07:00:00 |
| justify_interval(interval) | interval | 同时使用justify_days(interval)和justify_hours(interval) | select justify_interval(interval'1year 45days 343hour'); | 1 year 1 mon 29 days 07:00:00 |
| localtime | time | 当日时间 | select localtime; | 15:45:18.892224 |
| localtimestamp | timestamp | 当日日期和时间 | select localtimestamp; | 2016-07-08 15:46:55.181583 |
| make_date(year int, month int, day int) | date | 创建一个日期 | select make_date(2016,7,8); | 2016-07-08 |
|
make_interval( years int DEFAULT 0, months int DEFAULT 0, weeks int DEFAULT 0, days int DEFAULT 0, hours int DEFAULT 0, mins int DEFAULT 0, secs double precision DEFAULT 0.0) |
interval | 创建一个时间间隔 | select make_interval(1,hours=>3); | 1 year 03:00:00 |
|
make_time( hour int, min int, sec double precision) |
time | 创建一个时间 | select make_time(9,21,23); | 09:21:23 |
|
make_timestamp( year int, month int, day int, hour int, min int, sec double precision) |
timestamp | 创建一个时间戳 | select make_timestamp(2016,7,8,22,55,23.5); | 2016-07-08 22:55:23.5 |
|
make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ]) |
timestamp with time zone | 创建一个带有时区的时间戳 | select make_timestamptz(2016,7,8,22,55,23.5); | 2016-07-08 22:55:23.5-07 |
| now() | timestamp with time zone | 当前日期和时间 | select now(); | 2016-07-08 15:55:30.873537-07 |
| statement_timestamp() | timestamp with time zone | 同now() | select statement_timestamp(); | 2016-07-08 15:56:07.259956-07 |
| timeofday() | text |
当前日期和时间,包含周几, 功能与clock_timestamp()类似 |
select timeofday(); | Fri Jul 08 15:57:51.277239 2016 PDT |
| transaction_timestamp() | timestamp with time zone | 事务开始时的时间戳 | select transaction_timestamp(); | 2016-07-08 16:01:25.007153-07 |
| to_timestamp(double precision) | timestamp with time zone |
Convert Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp |
select to_timestamp(1284352323); |
2010-09-12 21:32:03-07 |
| pg_sleep(seconds double precision); |
当前会话休眠seconds秒 |
select pg_sleep(5); | ||
| pg_sleep_for(interval) | 当前会话休眠多长时间的间隔 | select pg_sleep_for('5 seconds'); | ||
| pg_sleep_until(timestamp with time zone) | 当前会话休眠至什么时间点 | select pg_sleep_until('2016-07-08 23:59:59'); |
postgresql----时间类型的更多相关文章
- PostgreSQL时间格式及相关函数实践
在创建表的时候,有客户需要将时间转为字符串,而且要求了具体的格式,如:20181115101010001.方便记录数据的更新时间,貌似是给Mysql使用,当时就很蛋疼,时间格式存储子啊数据库中就是va ...
- SQL Date 时间类型处理
SQL 日期(Dates) 2019-10-17 22:17:26 当我们处理日期时,最难的任务恐怕是确保插入的日期的格式,与数据库中日期列的格式相匹配. 保存的如果是日期部分,查询不会有太大问题 ...
- ASP.MVC时间类型json数据处理
服务端返回DateTime属性如果用自带的json方法返回的数据如下: 有2种办法解决一种是采用服务端解决方案,一种是使用前端解决方案 1.前端解决方案 第一步:对Date进行扩展 // 对Date的 ...
- Java中,关于字符串类型、随机验证码、 时间类型
一.字符串类型:String类型 定义一个字符串 String a="Hello World"; String b= new String ("Hello World&q ...
- Mysql时间类型处理
关于Mysql中时间的处理 最近在读<人类简史>,读第二遍.只有晚上睡觉之前读一点点,有时候觉得一天可以抽出一个专门的时间来看书了,效率应该能高不少. 另外分享个网址可以随心创作 这里有一 ...
- PHPExcel对于Excel中日期和时间类型的处理
PHPExcel是一款优秀的处理Excel文件读写的开源PHP Library,能够给我们提供强大的Excel读写能力,本文针对Excel处理过程中关于日期和时间类型的处理进行深入的讨论.PHPExc ...
- SQL Server的日期和时间类型
Sql Server使用 Date 表示日期,time表示时间,使用datetime和datetime2表示日期和时间. 1,秒的精度是指使用多少位小数表示秒 DateTime数据类型秒的精度是3,D ...
- java中时间类型的问题
时间类型:System.currentTimeMillis() 获得的是自1970-1-01 00:00:00.000 到当前时刻的时间距离,类型为longimport java.sql.Date d ...
- MySQL日期数据类型、时间类型使用总结
MySQL日期数据类型.时间类型使用总结 MySQL日期数据类型.MySQL时间类型使用总结,需要的朋友可以参考下. MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型 ...
- oracle 数据库时间类型为字符串 时间范围大小查询
select * from invoicedetail t2 where t2.Memo is null and to_char(to_date(t2.PrintDate,'yyyy-MM-dd hh ...
随机推荐
- [Cxf] spring-cxf 配置
1.在web.xml中配置servlet <!-- spring监听的配置 --> <listener> <listener-class>org.springfra ...
- e657. 用直线和曲线绘制图形
GeneralPath shape = new GeneralPath(); shape.moveTo(x, y); shape.lineTo(x, y); shape.quadTo(controlP ...
- 每个 case 语句的结尾不要忘了加 break,否则将导致多个分支重叠
每个 case 语句的结尾不要忘了加 break,否则将导致多个分支重叠 (除非有意使多个分支重叠). #include <iostream> /* run this program us ...
- Android camera 竖直拍照 获取竖直方向照片
竖直拍照 if (Integer.parseInt(Build.VERSION.SDK) >= 8) { camera.setDisplayOrientation(90); } else ...
- java面试题------40个Java集合面试问题和答案
Java集合框架为Java编程语言的基础,也是Java面试中非常重要的一个知识点. 这里,我列出了一些关于Java集合的重要问题和答案. 1.Java集合框架是什么?说出一些集合框架的长处? 每种编程 ...
- Unity3D - 详解Quaternion类(二)
OK,不做引子了,接上篇Unity3D - 详解Quaternion类(一)走起! 四.Quaternion类静态方法 Quaternion中的静态方法有9个即:Angle方法.Dot方法.Euler ...
- 详细分析css float 属性以及position:absolute 的区别
1.float 属性定义元素在哪个方向浮动.以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是何种元素.div一个典型的块 ...
- sftp,get命令使用*通配符的方式获取批量的文件
需求描述: 今天在使用sftp进行get文件的时候,有很多文件名类似的文件,以为还是需要一个一个get 后来发现get也可以使用通配符的方式进行匹配获取多个文件,在此记录下 操作过程: 1.通过sft ...
- Java类的设计----Object 类
Object类 Object类是所有Java类的根父类如果在类的声明中未使用extends关键字指明其父类,则默认父类为Object类 public class Person { ... } 等价于: ...
- [笔试题]黑板上写下50个数字,选两个黑板上数字a和b,在黑板写|b-a|,剩下的数字?
在黑板上写下50个数字:1至50.在接下来的49轮操作中,每次做如下操作:选取两个黑板上的数字a和b,擦去,在黑板上写|b-a|.请问最后一次动作之后剩下的数字可能是什么?为什么?(不用写代码,不写原 ...