Date of the Linux command I used-Display, modify system date Time
This article link: http://codingstandards.iteye.com/blog/1157513 (reproduced please specify the source)
Description of Use
The ATE command can be used to display and modify the system datetime, noting that it is not a time command.
Common parameters
Format: Date
Displays the current date time.
Format: Date mmddhhmm
Format: Date mmddhhmmyyyy
Format: Date MMDDHHMM.SS
Format: Date MMDDHHMMYYYY.SS
Set the current date time, only the root user can execute, and then execute clock-w to synchronize to the hardware clock.
MM is the month, DD is the date, HH is the number of hours, MM is the number of minutes, yyyy is the year, SS is the number of seconds.
Format: Date +format
Displays the current time according to the specified format. For example, date +%y-%m-%d displays the current date in the form of YYYY-MM-DD, where YYYY is the year, MM is the month, and DD is the date.
Common format
%Y yyyy format years (year)
%m mm Format month (), 01-12
%d DD format date (day of month), 01-31
%H hh Format hours (), 00-23
%M mm Format minutes (), 00-59
Number of seconds in the%s SS format (), 00-59
%F full date for YYYY-MM-DD format, same as%y-%m-%d
%T in HH-MM-SS Format (time), same as%h:%m:%s
%s Number of seconds since 1970. C function Time (&t) or Java System.currenttimemillis ()/1000, New Date (). GetTime ()/1000
%w week, 0-6,0 says Sunday
%u week, 1-7,7 said Sunday
Note that the above format can be any combination, can also include non-format strings, such as date "+ Today is%y-%d-%m, now is $h:%m:%s"
More formats man date or info date
Format: date-d STRING
Format: Date--date=string
Format: date-d STRING +format
Displays the datetime specified with string (display time described by STRING, not ' now ').
Format: Date-s STRING
Format: Date--set=string
Sets the date time specified for the current time as String.
The string is varied and supports a variety of date and time descriptions. Here are some commonly used date representations, hoping to be able to extrapolate.
Date specified:
Date-d YYYY-MM-DD
Specify the time, date is today:
Date-d HH:MM:SS
Specify date Time:
Date-d "Yyyy-mm-dd HH:MM:SS"
Specify the number of seconds since 1970:
Date-d ' 1970-01-01 1251734400 sec UTC ' (September 01, 2009 Tuesday 00:00:00 CST)
Date-d ' 1970-01-01 1314177812 sec UTC ' (Wednesday, August 24, 2011 17:23:32 CST)
Today:
Date
Date-d today
Date-d now
Tomorrow:
Date-d tomorrow
Date-d Next-day
Date-d next-days
Date-d "Next Day"
Date-d "Next Days"
Date-d "+1 Day"
Date-d "+1 Days"
Date-d "1 Day"
date-d "1 Days"
Date-d "-1 day Ago"
Date-d "-1 days Ago"
Yesterday:
Date-d yesterday
Date-d Last-day
Date-d last-days
Date-d "Last Day"
Date-d "Last Days"
Date-d "-1 day"
Date-d "-1 days"
date-d "1 day Ago"
date-d "1 days Ago"
Before:
Date-d "2 day Ago"
date-d "2 days Ago"
Date-d "-2 Day"
Date-d "-2 Days"
Big day Before:
date-d "3 day Ago"
date-d "3 days Ago"
Date-d "-3 Day"
Date-d "-3 days"
Last week, a week ago:
date-d "1 week Ago"
date-d "1 weeks Ago"
Last Friday (not last Friday):
Date-d "Last-friday"
Date-d "Last Friday"
Last month, January ago:
Date-d Last-month
Date-d last-months
Date-d "-1 month"
Date-d "-1 months"
Next month, after January:
Date-d Next-month
Date-d next-months
Date-d "+1 Month"
Date-d "+1 months"
Last year, a year ago:
Date-d Last-year
Date-d Last-years
Date-d "-1 year"
Date-d "-1 Years"
Next year, one year later:
Date-d Next-year
Date-d Next-years
Date-d "+1 Year"
Date-d "+1 Years"
An hour ago:
Date-d "Last-hour"
Date-d "Last-hours"
date-d "1 hour Ago"
date-d "1 hours Ago"
After one hour:
Date-d "1 Hour"
Date-d "1 Hours"
A minute ago:
date-d "1 minute Ago"
date-d "1 minutes Ago"
After one minute:
Date-d "1 Minute"
date-d "1 Minutes"
One second ago:
date-d "1 second Ago"
date-d "1 seconds Ago"
After one second:
Date-d "1 Second"
Date-d "1 Seconds"
Use example one to display and set date time
[[email protected] ct08]# date
Saturday, August 20, 2011 17:37:11 CST
[Email protected] ct08]# date 08220942
Monday, August 22, 2011 09:42:00 CST
[Email protected] ct08]# clock-w
[[email protected] ct08]# date
Monday, August 22, 2011 09:42:01 CST
[Email protected] ct08]#
Example two displays the specified date time
[Email protected] ~]#Date
August 23, 2011 Tuesday 07:41:03 CST
[Email protected] ~]#Date-d Next-day +%y%m%d
20110824
[Email protected] ~]#Date-d Next-day +%f
2011-08-24
[Email protected] ~]#Date-d next-day ' +%f%T '
2011-08-24 07:41:47
[Email protected] ~]#Date-d last-day ' +%f%T '
2011-08-22 07:43:46
[Email protected] ~]#
[Email protected] ~]# date-d yesterday ' +%f%T '
2011-08-22 07:44:31
[Email protected] ~]# date-d tomorrow ' +%f%T '
2011-08-24 07:45:19
[Email protected] ~]# date-d last-month +%y%m
201107
[Email protected] ~]# date-d next-month +%y%m
201109
[Email protected] ~]# date-d next-year +%y
2012
[Email protected] ~]#
Example three write a script to calculate the date of Mother's Day and Father's Day
Mother's Day (second Sunday of May every year)
May 8, 2005
May 14, 2006
May 13, 2007
May 11, 2008
May 10, 2009
May 9, 2010
May 8, 2011
May 13, 2012
Father's Day (third Sunday in June), following the father's holiday period in recent years
June 19, 2005
June 18, 2006
June 17, 2007
June 15, 2008
June 21, 2009
June 20, 2010
June 19, 2011
June 17, 2012
Here's how Linux looks at a month's calendar for a certain year.
[[email protected] ~]# Cal 5
May on 2012
Day 123456
1 2 3) 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29) 30 31
The following script is used to calculate the date of Mother's Day and Father's day for a specified year.
Bash script: calc_date.sh
Bash code
- #!/bin/sh
- # Mother's Day (second Sunday of May every year)
- # Usage:mother_day [Year]
- Mother_day ()
- {
- Local May1 # May 1
- If ["$"]; Then
- MAY1=$1-05-01 # can be a $1/05/01
- Else
- MAY1=5/1 # can be 05/01, but it can't be 05-01.
- Fi
- #date-D $may 1
- # See May 1 is the day of the week
- Local w=(date+may1) #%w 0 = Sunday 1-6= Monday to six
- #echo $w
- If [$w-eq 0]; Then # if it's May 1 Sunday, skip one weeks
- Date +%f-d "$may 1 +1 Week"
- else # If May 1 is not Sunday, skip two weeks and subtract w days
- Date +%f-d "may1+2wek− W Day "
- Fi
- }
- # Father's Day (third Sunday of June every year)
- # Usage:father_day [Year]
- Father_day ()
- {
- Local June1 # Save date of June 1
- If ["$"]; Then
- June1=$1-06-01
- Else
- June1=6/1
- Fi
- # Simplified calculation logic because 1-7 represents the day of the week
- Local w=(date+june1) #%u 7 = Sunday, 1-6 = Monday to six
- Date +%f-d "June1+3wek− W Day "
- }
- # usage:./calc_date.sh [Year]
- If ["$"]; Then
- Echo Mother day of the year $ is (motheRDay" /c12>1 ")
- Echo Father Day of the year $ is (fatHeRDay" /c12>1 ")
- Else
- Echo Mother day of this year is $ (mother_day)
- Echo Father Day of this year is $ (father_day)
- Fi
[Email protected] ~]#./calc_date.sh
Mother day of the year is 2011-05-08
Father Day of the year is 2011-06-19
[Email protected] ~]#./calc_date.sh 2011
Mother Day's 2011-05-08
Father Day's 2011-06-19
[Email protected] ~]#./calc_date.sh 2010
Mother Day's 2010-05-09
Father Day's 2010-06-20
[Email protected] ~]#/calc_date.sh 2009
Mother Day's 2009-05-10
Father Day's 2009-06-21
[Email protected] ~]#./calc_date.sh 2008
Mother Day's 2008-05-11
Father Day's 2008-06-15
[Email protected] ~]#./calc_date.sh
Mother Day's 2007-05-13
Father Day's 2007-06-17
[Email protected] ~]# /calc_date.sh 2006
Mother Day of the year 2006 was 2006-05-14
Father Day of the year 2006 was 2006-06-18
[Email protected] ~]# /calc_date.sh 2005
Mother Day of the year 2005 was 2005-05-08
Father Day of the year 2005 was 2005-06-19
[Email protected] ~]# ./calc_date.sh
Mother Day's 2012-05-13
Father Day's 2012-06-17
Problem thinking related information
"1" Tsung ' s Blog Linux uses date calculation time (Yesterday, tomorrow)
http://blog.longwin.com.tw/2010/07/linux-date-computer-2010/
"2" Baidu Space Linux date (formatted string)
Http://hi.baidu.com/wuchangqian/blog/item/925281174587c65df3de32b0.html
"3" Bird's Linux private cuisine 15.3.1 Linux manual work: date, Hwclock
Http://linux.vbird.org/linux_server/0440ntp.php#date
"4" Xukaizijian [Linux] date and Hwclick
Http://xukaizijian.blog.163.com/blog/static/1704331192011028103527980/?suggestedreading
"5" blog (ppp_10001) linux--date command
http://blog.csdn.net/ppp_10001/article/details/5275594