date usage in shell programming

Convert time string to timestamp with shell

 

date +%s can get the UNIX timestamp;
use the shell to convert the time string to the timestamp:
      date -d "2010-10-18 00:00:00" +%s The output is like: 1287331200
and the timestamp is converted For strings, you can do this:
      date -d @1287331200 "+%Y-%m-%d" The output looks like: 2010-10-18
If you need to get the days before and after the specified date, you can:
      1. seconds=`date -d "2010-10-18 00:00:00" +%s` #Get timestamp
      2, seconds_new=`expr $seconds + 86400` #Add the number of seconds in a day 86400
      3, date_new=`date -d @ $seconds_new "+%Y-%m-%d"` #Get the day before the specified day plus one day before

 

 

1、date --help

%% Outputs the % symbol a literal %
%a The current locale's abbreviated weekday name (Sun..Sat)
%A The current locale's full weekday name, variable length (Sunday..Saturday)
%b The current locale locale's abbreviated month name (Jan..Dec)
%B current domain's full month name locale's full month name, variable length (January..December)
%c current domain's default time format locale's date and time (Sat Nov 04 12 :02:33 EST 1989)
%C n century century (year divided by 100 and truncated to an integer) [00-99]
%d two-digit day of month (01..31)
%D short time format date ( mm/dd/yy)
%e short format day of month, blank padded ( 1..31)
%F file time format same as %Y-%m-%d
%g the 2-digit year corresponding to the %V week number
%G the 4-digit year corresponding to the %V week number
%h same as %b
%H 24-hour hour (00..23)
%I 12-hour hour (01..12)
%j day of year (001..366)
%k short format 24-hour hour ( 0..23)
%l short 12-hour hour ( 1..12)
%m two-digit month month (01..12)
%M two-digit minute (00.. 59)
%n a newline
%N nanoseconds nanoseconds (000000000..999999999)
%p upper case AM or PM indicator locale's upper case AM or PM indicator (blank in many locales)
%P lower case current locale's lower case am or pm indicator (blank in many locales)
%r 12-hour time representation (hour:minute:second, double digits) time, 12-hour (hh:mm:ss [AP] M)
%R 24-hour time representation (hour:minute, double digits) time, 24-hour (hh:mm)
%s seconds from the base time 1970-01-01 00:00:00 to the current moment since `00:00:00 1970-01-01 UTC' (a GNU extension)
%S double second second (00..60); the 60 is necessary to accommodate a leap second
%t Horizontal tab stop (tab) a horizontal tab
%T 24-hour time represents time, 24-hour (hh:mm:ss)
%u Number of week (1-7 starting from Monday) day of week ( 1..7); 1 represents Monday
%U Week number of year with Sunday as first day of week (00..53)
%V Week number of year with Monday as first day of week start week number of year with Monday as first day of week (01..53)
%w day of week with Sunday as start 0-6 day of week (0..6); 0 represents Sunday
%W in year week number of year with Monday as first day of week (00..53)
%x locale's date representation (mm/dd/yy)
%X locale's time representation (% H:%M:%S)
%y last two digits of year (00..99)
%Y year year (1970…)
%z field represented in RFC-2822 standard time format RFC-2822 style numeric timezone (-0500) (a nonstandard extension)
%Z 时间域 time zone (e.g., EDT), or nothing if no time zone is determinable

By default, date pads numeric fields with zeroes. GNU date recognizes
the following modifiers between `%’ and a numeric directive.

`-’ (hyphen) do not pad the field
`_’ (underscore) pad the field with spaces

--------------------------------------------------------------------------------

2. Some usage

1) #Output the current moment 43 days ago in the format of yymmdd

date +%Y%m%d --date='43 days ago'       

 

2)# 测试十亿分之一秒
date +’%Y%m%d %H:%M:%S.%N’;date +’%Y%m%d %H:%M:%S.%N’;date +’%Y%m%d %H:%M:%S.%N’;date +’%Y%m%d %H:%M:%S.%N’

3) #Create a directory with the current time as the file name
mkdir `date +%Y%m%d`

 

4) #Backup tar with time as file name
-cvf ./htdocs`date +%Y%m%d`.tar ./*

 

5) # skip the line after displaying the time, and then display the current date 

date +%T%n%Y%m%d

 

6) #Only display the month and day 

date +%B%d

 

7) #Get last week's date (day, month, year, hour)

date -d "-1 week" +%Y%m%d   

 

8)#获取24小时前日期

date --date="-24 hour" +%Y%m%d

 

9)#shell脚本里面赋给变量值

date_now=`date +%s`

 

10)#计算执行一段sql脚本的运行时间

 

TIME_BEGIN=$(date '+%s.%N')
$sqlcli < queries/q1.3.sql 1>> $FILE_RESULT  2>> $FILE_ERROR
TIME_END=$(date '+%s.%N')
TIME_RUN=$(awk 'BEGIN{print '$TIME_END' - '$TIME_BEGIN'}')

 

11)#编写shell脚本计算离自己生日还有多少天?

    read -p "Input your birthday(YYYYmmdd):" date1

  m=`date --date="$date1" +%m`    #得到生日的月

  d=`date --date="$date1" +%d`    #得到生日的日

  date_now=`date +%s`             #得到当前时间的秒值

  y=`date +%Y`                    #得到当前时间的年

  birth=`date --date="$y$m$d" +%s`      #得到今年的生日日期的秒值

  internal=$(($birth-$date_now))        #计算今日到生日日期的间隔时间

  if [ "$internal" -lt "0" ]; then             #判断今天的生日是否已过

  birth=`date --date="$(($y+1))$m$d" +%s`      #得到明天的生日日期秒值

  internal=$(($birth-$date_now))               #计算今天到下一个生日的间隔时间

  fi

  echo "There is :$((einternal/60/60/24)) days."       #输出结果,秒换算为天

 

 

12)#若是不以加号作为开头,则表示要设定时间,而时间格式为 MMDDhhmm[[CC]YY][.ss],

其中 MM 为月份,

DD 为日,

hh 为小时,

mm 为分钟,

CC 为年份前两位数字,

YY 为年份后两位数字,

ss 为秒数

 

13)

#显示目前的格林威治时间,也叫“世界时”。是英国的标准时间,也是世界各地时间的参考标准。中英两国的标准时差为8个小时,即英国的当地时间比中国的北京时间晚8小时。

date -u              
Thu Sep 28 09:32:04 UTC 2006

 

14)#修改时间

date -s
按字符串方式修改时间
可以只修改日期,不修改时间,输入: date -s 2007-08-03
只修改时间,输入:date -s 14:15:00
同时修改日期时间,注意要加双引号,日期与时间之间有一空格,输入:date -s "2007-08-03 14:15:00"

修改完后,记得输入:clock -w
把系统时间写入CMOS

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326983202&siteId=291194637