Date usage in shell programming (GO)

Source: Internet
Author: User
Tags local time locale month name rfc

Original address: http://blog.sina.com.cn/s/blog_61c006ea0100mgxe.html

1. Date--help

Percent output% sign a literal%
%a the current domain of the week abbreviation locale ' s abbreviated weekday name (Sun.. Sat)
%A the current domain's full weekday name, variable length (Sunday. Saturday)
%b the current domain's month abbreviation locale ' s abbreviated month name (Jan. DEC)
%B the full month of the current domain locale's full month name, variable length (January. December)
The default time format for the current field of%c, locale ' s date and times (Sat 12:02:33 EST1989)
%c N Centennial Century (year divided by + truncated to an integer) [00-99]
%d two-bit 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 Hour (00..23)
%I 12-hour Hour Hour (01..12)
%j Day of the Year (001..366)
%k short Format 24-hour Hour Hour (0..23)
%l Short Format 12-hour Hour Hour (1..12)
%m Two-digit months month (01..12)
%M double-bit min minute (00..59)
%n Line A newline
%N one-zero sec nanoseconds (000000000..999999999)
%p uppercase The current field indicates the locale ' s upper case AM or PM indicator (blank inmany locales)
%P lowercase current field indicates locale ' s lower case AM or PM indicator (blank inmany locales)
%r 12-hour Time representation (time: minute: Second, double bit) times, 12-hour (Hh:mm:ss [ap]m)
%R 24-hour time representation (time: minute, double), 24-hour (hh:mm)
%s from 1970-01-01 00:00:00 to the current time in seconds seconds since ' 00:00:001970-01-01 UTC ' (a GNU extension)
%s Double-bit seconds second (00..60); The necessary to accommodate a leapsecond
%t horizontal tab stop (tab) a horizontal tab
%T 24-hour time representation, 24-hour (HH:MM:SS)
%u numbers represent the week (starting from Monday 1-7) Day of Week (1..7); 1 represents Monday
%u week Sunday for start week number of year with Sunday as first day Ofweek (00..53)
%V Week of Monday for start week number of year with Monday as first day Ofweek (01..53)
%w days of the week Sunday for the beginning of 0-6 day of Week (0..6); 0 represents Sunday
%W Week of Monday for start week number of year with Monday as first day Ofweek (00..53)
%x local date format locale ' s date representation (MM/DD/YY)
%x local time format locale ' s representation (%h:%m:%s)
%y two-bit years last two digits of year (00..99)
%Y year (1970 ...)
%z RFC-2822 Standard Time format representation of the domain RFC-2822 style numeric timezone ( -0500) (Anonstandard extension)
%Z time zone (e.g., EDT), or nothing if no time zone isdeterminable

By default, the date pads numeric fields with zeroes. GNU daterecognizes
The following modifiers between '% ' and a numeric directive.

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

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

2. Some usage

1) #以yymmdd的格式输出43天前的当前时刻

Date +%y%m%d--date= ' Daysago '

2) # test One-zero seconds
Date + '%y%m%d%h:%m:%s.%n ';d ate + '%y%m%d%h:%m:%s.%n ';d ate + '%y%m%d%h:%m:%s.%n ';d ate + '%y%m%d%h:%m:%s.%n '

3) #创建以当前时间为文件名的目录
mkdir ' Date +%y%m%d '

4) #备份以时间做为文件名的
TAR-CVF./htdocs ' Date +%y%m%d '. Tar./*

5) #显示时间后跳行 and display the current date

Date +%t%n%y%m%d

6) #只显示月份与日数

Date +%b%d

7) #获取上周日期 (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 '

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

time_begin=$ (date ' +%s.%n ')
$sqlcli < queries/q1.3.sql1>> $FILE _result 2>> $FILE _error
time_end=$ (date ' +%s.%n ')
time_run=$ (awk ' begin{print ' $TIME _end '-' $TIME _begin '} ')

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

Read-p "Input your Birthday (YYYYMMDD):" Date1

m= ' Date--date= ' $date 1 "+%m ' #得到生日的月

d= ' Date--date= ' $date 1 "+%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." #输出结果, seconds conversion to days

#若是不以加号作为开头, the time format is set to MMDDHHMM[[CC]YY][.SS],

Where MM is the month,

DD is the day,

HH for hours,

MM for minutes,

CC is the first two digits of the year,

YY is two digits after the year,

SS is the number of seconds

13)

#显示目前的格林威治时间, also known as the "universal". It is the standard time of the United Kingdom and the reference standard for Time around the world. The standard Time difference between China and Britain is 8 hours, that is, the UK is 8 hours behind the time in Beijing .

Date-u
Thu Sep 09:32:04 UTC 2006

) #修改时间

Date-s
Modify Time by string
Can only modify the date, do not modify the time, enter: Date-s 2007-08-03
Only modified time, input: Date-s 14:15:00
Also modify the date time, note to add double quotation marks, the date and time there is a space, enter: Date-s "2007-08-03 14:15:00"

After modifying, remember to enter: Clock-w
Write system time to CMOS

Date usage in shell programming (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.