Linux command Detailed date usage (Calculate Mother's Day and father's holiday script sample) _linux

Source: Internet
Author: User


Common parameters
Format: Date
Displays the current date time.

Format: Date mmddhhmm # In short, it's the day of the month.
Format: Date mmddhhmmyyyy
Format: Date MMDDHHMM.SS
Format: Date MMDDHHMMYYYY.SS
Set the current date time, only the root user can execute, and then perform clock-w to sync 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 is displaying the current date in yyyy-mm-dd form, where YYYY is the year, MM is the month, and DD is the date.

Common format
%Y yyyy format (year)
%m mm-formatted month (), 01-12
%d DD format date (day of month), 01-31
%H hh Format hours (), 00-23
%m mm Format minutes (), 00-59
%s SS-formatted seconds (), 00-59
Full date%F yyyy-mm-dd format, same as%y-%m-%d
%T hh-mm-ss Format (time), same%h:%m:%s
%s Number of seconds since 1970. C function Time (&t) or Java System.currenttimemillis ()/1000, New Date (). GetTime ()/1000
%w Day of the week, 0-6,0 said Sunday
%u days of the week, 1-7,7 said Sunday
Note that the above format can be any combination, can also include the unformatted string, such as date "+ Today is%y-%d-%m, now is $h:%m:%s"
More format 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 by the current time as String.

string is varied and supports a description of many dates and times. The following is a list of common date representations that are expected to be extrapolate.
Date specified:
Date-d YYYY-MM-DD
Specified time, date is today:
Date-d HH:MM:SS
Specified 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:
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, January later:
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, a 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"
A minute later:
Date-d "1 Minute"
date-d "1 Minutes"
A second ago:
date-d "1 second Ago"
date-d "1 seconds Ago"
After one second:
Date-d "1 Second"
Date-d "1 Seconds"

Using the sample
Example one shows and sets the date time
[Root@node56 ct08]# Date
Saturday, August 20, 2011 17:37:11 CST
[Root@node56 ct08]# Date 08220942
Monday, August 22, 2011 09:42:00 CST
[Root@node56 ct08]# Clock-w
[Root@node56 ct08]# Date
Monday, August 22, 2011 09:42:01 CST
[Root@node56 ct08]#

Example two shows the specified date time
[Root@node56 ~]# Date
August 23, 2011 Tuesday 07:41:03 CST
[root@node56 ~]# date-d next-day +%y%m%d
20110824
[root@node56 ~]# date-d next-day +%f
2011-08-24
[root@node56 ~]# date-d next-day ' +%f%T '
2011-08-24 07:41:47
[root@node56 ~]# date-d last-day ' +%f%T '
2011-08-22 07:43:46
[Root@node56 ~]#
[Root@node56 ~]# date-d yesterday ' +%f%T '
2011-08-22 07:44:31
[Root@node56 ~]# date-d tomorrow ' +%f%T '
2011-08-24 07:45:19
[root@node56 ~]# date-d last-month +%y%m
201107
[root@node56 ~]# date-d next-month +%y%m
201109
[root@node56 ~]# date-d next-year +%y
2012
[Root@node56 ~]#

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, June), the following is the father's holiday season 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

The following is how Linux looks at the calendar for a particular month of the year.
[Root@node56 ~]# Cal 5 2012
May 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 the specified year.
Bash script: calc_date.sh

Copy Code code as follows:

#!/bin/sh

# Mother's Day (the second Sunday of May every year)
# Usage:mother_day [Year]
Mother_day ()
{
Local May1 # May 1
If ["$"]; Then
MAY1=$1-05-01 # can also be $1/05/01
Else
MAY1=5/1 # can also be 05/01, but not 05-01.
Fi
#date-D $may 1
# See May 1 is the day of the week
Local w=$ (date +%w-d $may 1) #%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 "$may 1 +2 week-$w Day"
Fi
}

# Father's Day (third Sunday of June)
# usage:father_day [year]
Father_day ()
{
         Local june1     # save June 1 Date
        if [' $]; then< br>                june1=$1-06-01
        Else
                 june1=6/1
        fi
         because using 1-7 for the day of the week simplifies the calculation logic
        local w=$ (date +%u-d $june 1)    #%u 7 = Sunday, 1-6 = Monday to six
        date +%f-d $june 1 +3 Week -$w Day '
}

# usage:./calc_date.sh [Year]
If ["$"]; Then
Echo Mother Day of Year $ is $ (mother_day "$")
Echo Father Day of Year $ is $ (father_day "$")
Else
Echo Mother Day of this year is $ (mother_day)
Echo Father Day of this year is $ (father_day)
Fi

The results of the operation are as follows:

Copy Code code as follows:

[Root@node56 ~]#./calc_date.sh
Mother Day of this year is 2011-05-08
Father Day of this year is 2011-06-19
[root@node56 ~]#./calc_date.sh 2011
Mother Day of the 2011-05-08
Father Day of the 2011-06-19
[Root@node56 ~]#./calc_date.sh 2010
Mother Day of the 2010-05-09
Father Day of the 2010-06-20
[root@node56 ~]#./calc_date.sh 2009
Mother Day of the 2009 is 2009-05-10
Father Day of the 2009 is 2009-06-21
[Root@node56 ~]#./calc_date.sh 2008
Mother Day of the 2008 is 2008-05-11
Father Day of the 2008 is 2008-06-15
[Root@node56 ~]#./calc_date.sh 2007
Mother Day of the 2007 is 2007-05-13
Father Day of the 2007 is 2007-06-17
[Root@node56 ~]#./calc_date.sh 2006
Mother Day of the 2006 is 2006-05-14
Father Day of the 2006 is 2006-06-18
[Root@node56 ~]#./calc_date.sh 2005
Mother Day of the year, 2005-05-08
Father Day of the year, 2005-06-19
[Root@node56 ~]#./calc_date.sh 2012
Mother Day of the 2012-05-13
Father Day of the 2012-06-17

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.