The linux command details the script example date USES method of to calculate the mother's day and father's day periods

  • 2020-05-07 20:45:37
  • OfStack


Commonly used parameters
Format: date
Displays the current date and time.

date mmddHHMM                  
Format: date mmddHHMMYYYY
Format: date mmddHHMM.SS
Format: date mmddHHMMYYYY.SS
Set the current date time so that only root users can execute, and then execute clock-w to synchronize to the hardware clock.
mm is the month, dd is the date, HH is the hours, MM is the minutes, YYYY is the year, SS is the 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.

Commonly used FORMAT
%Y   YYYY year (Year)
%m   mm months (), 01-12
Date in d     dd format (day of month), 01-31
%H     HH number of hours (), 00-23
%M   MM number of minutes (), 00-59
%S     SS number of seconds (), 00-59
%F     es95-dd full date (Full date), same as %Y-%m-%d
%T     es106-MM SS format time (Time), same as %H:%M:%S
%s     seconds since 1970. C functions time(&t) or System.currentTimeMillis ()/1000, new Date().getTime ()/1000
What day of the week is w    ? 0-6, 0 means Sunday
What day of the week is it? 1-7, 7 means Sunday
Note that the above formats can be combined arbitrarily, and can also include non-format strings, such as date "+ today %Y-%d-%m, now $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 date and time specified by STRING (display time described by STRING, not 'now').

Format: date-s STRING
Format: date --set=STRING
Set the current time to the date time specified by STRING.

STRING is very versatile and supports many ways of describing dates and times. Here is a list of some common ways to represent dates, and I hope to be able to invert 1 to 3.
Specified date:
date -d YYYY-mm-dd
Specified time and date is today:
date -d HH:MM:SS
Specified date and time:
date -d "YYYY-mm-dd HH:MM:SS"
Specifies the number of seconds since 1970:
date -d '1970-01-01 1251734400 sec utc'           (Monday, 01 September 2009 00:00:00 CST)
date -d '1970-01-01 1314177812 sec utc'         (Monday, 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"
The day before yesterday:
date -d "2 day ago"
date -d "2 days ago"
date -d "-2 day"
date -d "-2 days"
Three days ago.
date -d "3 day ago"
date -d "3 days ago"
date -d "-3 day"
date -d "-3 days"
Last week, 1 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, before January:
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, 1 year ago:
date -d last-year
date -d last-years
date -d "-1 year"
date -d "-1 years"
Next year, 1 year from now:
date -d next-year
date -d next-years
date -d "+1 year"
date -d "+1 years"
1 hour ago:
date -d "last-hour"
date -d "last-hours"
date -d "1 hour ago"
date -d "1 hours ago"
1 hour later:
date -d "1 hour"
date -d "1 hours"
1 minute ago:
date -d "1 minute ago"
date -d "1 minutes ago"
1 minute later:
date -d "1 minute"
date -d "1 minutes"
1 second before:
date -d "1 second ago"
date -d "1 seconds ago"
1 seconds after:
date -d "1 second"
date -d "1 seconds"

Use the sample
Example 1 shows and sets the date time
[root@node56 ct08]# date
Sunday, August 20, 2011 17:37:11 CST
[root@node56 ct08]# date 08220942
Sunday, August 22, 2011 09:42:00 CST
[root@node56 ct08]# clock -w
[root@node56 ct08]# date
Sunday, 22 August 2011 09:42:01 CST
[root@node56 ct08]#

Example 2 shows the specified date and time
[root@node56 ~]# date
Sunday, August 23, 2011 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 3 write a script to calculate the dates of mother's day and father's day
Mother's day (2nd Sunday in May)
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 (3rd Sunday in June), the following is the most recent father's day period
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 views the calendar for a certain year and a certain month.
[root@node56 ~]# cal 5 2012
        May 2012          
One, two, three, four, five, six
            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 dates of mother's day and father's day for the specified year.
Bash script: calc_date.sh


#!/bin/sh
#  Mother's day (every year 5 On the first 2 On a Sunday   ) 
# usage: mother_day [year]
mother_day()
{
        local may1  # 5 month 1 day 
        if [ "$1" ]; then
                may1=$1-05-01   #  It could be $1/05/01
        else
                may1=5/1   #  It can also be 05/01 But it can't be 05-01
        fi
        #date -d $may1
        #  see 5 month 1 What day is the day of the week 
        local w=$(date +%w -d $may1)    # %w 0= Sunday  1-6= week 1 to 6
        #echo $w
        if [ $w -eq 0 ]; then   #  If it is 5 month 1 Sunday, just skip 1 A few weeks 
                date +%F -d "$may1 +1 week"
        else    #  if 5 month 1 If it's not Sunday, just skip two weeks and subtract w day 
                date +%F -d "$may1 +2 week -$w day"
        fi
}
#  Father's day (every year 6 On the first 3 Last Sunday) 
# usage: father_day [year]
father_day()
{
        local june1     #  save 6 month 1 The date of the day 
        if [ "$1" ]; then
                june1=$1-06-01
        else
                june1=6/1
        fi
        #  Because the 1-7 Represents the day of the week, simplifying the logic 
        local w=$(date +%u -d $june1)   # %u 7= Sunday 1-6= week 1 to 6
        date +%F -d "$june1 +3 week -$w day"
}
# usage: ./calc_date.sh [year]
if [ "$1" ]; then
        echo Mother Day of year $1 is $(mother_day "$1")
        echo Father Day of year $1 is $(father_day "$1")
else
        echo Mother Day of this year is $(mother_day)
        echo Father Day of this year is $(father_day)
fi

The operation results are 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 year 2011 is 2011-05-08
Father Day of year 2011 is 2011-06-19
[root@node56 ~]# ./calc_date.sh 2010 
Mother Day of year 2010 is 2010-05-09
Father Day of year 2010 is 2010-06-20
[root@node56 ~]# ./calc_date.sh 2009 
Mother Day of year 2009 is 2009-05-10
Father Day of year 2009 is 2009-06-21
[root@node56 ~]# ./calc_date.sh 2008 
Mother Day of year 2008 is 2008-05-11
Father Day of year 2008 is 2008-06-15
[root@node56 ~]# ./calc_date.sh 2007 
Mother Day of year 2007 is 2007-05-13
Father Day of year 2007 is 2007-06-17
[root@node56 ~]# ./calc_date.sh 2006 
Mother Day of year 2006 is 2006-05-14
Father Day of year 2006 is 2006-06-18
[root@node56 ~]# ./calc_date.sh 2005 
Mother Day of year 2005 is 2005-05-08
Father Day of year 2005 is 2005-06-19
[root@node56 ~]# ./calc_date.sh 2012 
Mother Day of year 2012 is 2012-05-13
Father Day of year 2012 is 2012-06-17


Related articles: