The date usage of the shell

Source: Internet
Author: User
Tags local time month name

#man Date can see the Help file for date
#date Get the current time
#date-D " -1week" +%y%m%d access to the Sunday period (day,month,year,hour)
#date--date= " -24 hour" +%y%m%d ditto
date_now= ' date+%s ' shell script assigns variable values
%%% OUTPUT% symbol
%a the current domain's weekday abbreviation (Sun ... Sat)
%A the current domain for the full week (Sunday. Saturday)
%B the month abbreviation for the current field. DEC)
%B the full month name of the current domain (January. December)
Default time format for current field in%c (Sat Nov 12:02:33 EST 1989)
%c N Hundred years [00-99]
%d two-bit days (01..31)

%d Short time Format (MM/DD/YY)

%e Short Format Day (1..31)

%F file time format same as%y-%m-%d

%h Same as%b

%H 24-hour hours (00..23)

%I 12-hour hours (01..12)

%j Day of the Year (001..366)

%k Short Form 24 hour (0..23)

%l Short Form 12 Hour (1..12)

%m Two-digit month (01..12)

%m double digit minute (00..59)

%n Line Change

%N One-zero seconds (000000000..999999999)

%p Upper afternoon indication of the current field in uppercase (blank in many locales)

%P on the upper PM of the current field of lowercase (blank in many locales)

%r 12-hour Time representation (time: minutes: seconds, double digits) times, 12-hour (Hh:mm:ss [ap]m)
%R 24-hour time representation (time: minutes, double digits) times, 24-hour (hh:mm)
%s from base time 1970-01-01 00:00:00 to current time seconds (a GNU extension)
%s Dual-digit seconds second (00..60);
%t horizontal tab (tab)
%T 24-hour time representation (HH:MM:SS)
%u number of weeks represented (starting from Monday 1-7)
%u the first weeks of the year Sunday as the beginning (00..53)
%V the first few weeks of the year Monday as the beginning (01..53)
%w Day of the week of Sunday as the beginning (0..6)
%w the first few weeks of the year Monday as the beginning (00..53)
%x Local date format (MM/DD/YY)
%x local time format (%h:%m:%s)
%y Two-bit year (00..99)
%Y year (1970 ...)
Example: Write a shell script to calculate how many days from your birthday.

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 ' #得到今年的生日日期 The second value
internal=$ (($birth-$date _now)) #计算今日到生日日期的间隔时间
if ["$internal"-lt "0"]; then #判断今天的生日是否已过
birth= ' d Ate--date= "$ (($y + 1)) $m $d" +%s "#得到明天的生日日期秒值
internal=$ (($birth-$date _now)) #计算今天到下一个生日的间隔时间
fi
echo" Thereis: $ ((einternal/60/60/24)) days. "#输出结果, second conversion to day

To create a file name with the current time

mkdir ' date+%y%m%d '

Backup takes time as the filename

Tar cvf./htdocs ' date +%y%m%d '. Tar./*

How does the date command get the dates of the previous week?

Lastmonth= ' Date+%y-%m--date= '-1 month "'"-01 ";

The awk-v is then passed as a parameter in a simple point.

#date-D " -1week" "+%y%m%d%A"

Or: #date-D "Last week" +%y%m%d

Extrapolate: Date order obtained the day before yesterday, tomorrow, the day after, last month, next month

Date-d "-2 Day" "+%y%m%d%A" or date--date= "2 days Ago" "+%y%m%d%A"

Date-d "-1 Day" "+%y%m%d%A" or date--date= "1 day Ago" "+%y%m%d%A"

Date-d "1 Day" "+%y%m%d%A"

Date-d "2 Day" "+%y%m%d%A"

Date-d "-1 month" "+%y%m%d%A"

Date-d "1 Month" "+%y%m%d%A"

Echo ' date-d '-1 month "" +%y-%m-%d%x "

There are other uses:

Date+%s can get the time stamp of Unix;

Known timestamp, ask for current date
Date--date= "19700101 +$ (((' Date +%s ') + (8*60*60))) sec" +%y%m%d ""%h:%m:%s
PS: Starting from January 01, 1970, +$ (((' Date +%s ') + (8*60*60)) The SEC indicates how many seconds have passed, in which (8*60*60) represents 8 hours, time zone problem, China time and world time difference 8 hours.

Turn from: http://linux.chinaunix.net/bbs/forum-2-1.html

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.