Shell's date usage

Source: Internet
Author: User
Tags local time month name

#man date to see the Help file for date

#date Get the current time

#date-D " -1week" +%y%m%d get last Sunday issue (Day,month,year,hour)

#date--date= " -24 hour" +%y%m%d Ibid.

date_now= ' date+%s ' shell script to assign variable values

Percent output% symbol

%A the weekday abbreviation of the current domain (Sun.. Sat)

%A the current field of the week full write (Sunday. Saturday)

%B the month abbreviation for the current field (Jan. DEC)

%B the full month name of the current field (January). December)

The default time format for the current domain in%c (Sat 12:02:33 EST 1989)

%c n Centennial [00-99]

%d two-bit days (01..31)

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

%e short format days (1..31)

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

%h Same as%b

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

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

%j the first day of the Year (001..366)

%k short Format 24-hour hour (0..23)

%l Short Format 12-hour hour (1..12)

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

%M double-bit minutes (00..59)

%n line break

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

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

%P the previous afternoon indication of the current field in lowercase (blank in many 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 (a GNU extension)

%s Double-bit seconds second (00..60);

%t horizontal tab stop (tab)

%T 24-hour time representation (HH:MM:SS)

%u number expressed in weeks (starting from Monday 1-7)

%u the first weeks of the year Sunday for start (00..53)

%V the first week of the year Monday for the beginning (01..53)

%w Day of the week Sunday for Start (0..6)

%W the first week of the year Monday for 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: Writing a shell script calculate how many days away 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 ' #得到今年的生日日期的秒值
internal=$ (($birth-$date _now)) #计算今日到生日日期的间隔时间
If ["$internal"-lt "0"]; Then #判断今天的生日是否已过
Birth= ' Date--date= "$ (($y + 1)) $m $d" +%s "#得到明天的生日日期秒值
internal=$ (($birth-$date _now)) #计算今天到下一个生日的间隔时间
Fi
echo "Thereis: $ ((einternal/60/60/24)) days." #输出结果, seconds conversion to day

Creates a file with the current time as the filename.

mkdir ' date+%y%m%d '

The backup takes the time as the file name

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

How does the date command get dates from last week?

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

Then awk-v as a parameter to pass in a simple point.

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

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

Extrapolate: Date command gets 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 Unix timestamp;

Time stamp known for the current date

Date--date= "19700101 +$ (($ ((' date +%s ') + (8*60*60))] sec" +%y%m%d ""%h:%m:%s

PS: Counting from January 01, 1970, +$ (($ (' Date +%s ') + (8*60*60))) SEC indicates how many seconds, where (8*60*60) represents 8 hours, time zone problem, China time is 8 hours from world time.

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

Shell's date usage

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.