linux下 date命令实现 日期与时间戳互相转换

A.将日期转换为Unix时间戳
将当前时间以Unix时间戳表示:

  1. date +%s
输出如下:
1361542433
转换指定日期为Unix时间戳:

  1. date -d '2013-2-22 22:14' +%s
输出如下:
1361542440
B.将Unix时间戳转换为日期时间
不指定日期时间的格式:

  1. date -d @1361542596
输出如下:
Fri Feb 22 22:16:36 CST 2013
指定日期格式的转换:

  1. date -d @1361542596 +"%Y-%m-%d %H:%M:%S"
输出如下:
2013-02-22 22:16:36



1.查看指定时间的时间戳
 
 查看当前时间
 #date +%s
 
 查看指定时间
 #date -d 2008-01-01 +%s  
 1199116800
 #date -d 20080101 +%s
 1199116800
 
2.将时间戳转换成date
 #date -d '1970-01-01 UTC 1199116800 seconds'
 2008年 01月 01日 星期二 00:00:00 CST
 
 #export LANG=en
 #date -d '19700101 UTC 1199116800 seconds'  
 Tue Jan  1 00:00:00 CST 2008

你可能感兴趣的:(linux,命令)