Linux Script - Date Manipulation

I will set one date variable (Say '08 -JUN-2011 '), and I want to do some calculations based on that date, namely
1. You need to get the first day of this month.
2. The previous date of the given date of the month.
3. The last day of the given date of the month.

Everything that I know manipulates using the current system date and time, but does not know how to implement it with a user-set date. I need this to be achieved using the linux shell script.

Any help would be appreciated.

Thanks,
Kartik

+5
source share
5 answers

Here's how to perform manipulations using the GNU date:

#!/bin/sh USER_DATE=JUN-08-2011 # first day of the month FIRST_DAY_OF_MONTH=$(date -d "$USER_DATE" +%b-01-%Y) PREVIOUS_DAY=$(date -d "$USER_DATE -1 days" +%b-%d-%Y) # last day of the month FIRST_DAY_NEXT_MONTH=$(date -d "$USER_DATE +1 month" +%b-01-%Y) LAST_DAY_OF_MONTH=$(date -d "$FIRST_DAY_NEXT_MONTH -1 day" +%b-%d-%Y) echo "User date: $USER_DATE" echo "1. First day of the month: $FIRST_DAY_OF_MONTH" echo "2. Previous day: $PREVIOUS_DAY" echo "3. Last day of the month: $LAST_DAY_OF_MONTH" 

Output:

 User date: JUN-08-2011 1. First day of the month: Jun-01-2011 2. Previous day: Jun-07-2011 3. Last day of the month: Jun-30-2011 
+9
source

This will be minimized in a shell script. You are better off using Date :: Manip in perl or something similar in another full-featured language. However, I can come up with some ways to do this with the date command. First of all, you can use the -date parameter to set the starting point for the date, for example:

  $ date --date = '08 -JUN-2011 '
 Wed Jun 8 00:00:00 EDT 2011

You can get the previous date as follows:

  $ date --date = '08 -JUN-2011 -1 days'
 Tue Jun 7 00:00:00 EDT 2011

On the last day of the month, I just return from 31 to the date, not failing. Can you check $? for this

  $ date --date = '31 -JUN-2011 '; echo $?
 date: invalid date `31-JUN-2011 '
 one
 $ date --date = '30 -JUN-2011 '; echo $?
 Thu Jun 30 00:00:00 EDT 2011
 0

On the first day of the month ... it's usually 01 :)

+3
source

Since you are using Linux, hopefully, you have the GNU date function available. It can handle almost any description of the relative date you are thinking of.

Here are some examples.

 date --date="last month" +%Y-%m-%d date --date="yesterday" +%Y-%m-%d date --date="last month" +%b x=$(date --date "10 days ago" +%Y/%m/%d) 

To learn more about this, see GNU Examples .

Once you use it, you can relate the collection of information using date --help , which shows all the main parameters (but sometimes they are difficult to interpret).

Hope this helps.

+3
source
 $ date +%m/01/%Y 

I came here to find a way to get the first day of the current month.

0
source

Using the dateutils dround method:

Current month:

 $ dround today -1 2014-02-01 

Previous month

 $ dround today -31 2014-01-31 

Last day of the current month:

 $ dround today +31 2014-02-28 

Of course, you can use a custom date instead of today , the idea is to round up to the desired day of the month, for example. next first month of the month 2010-10-04:

 $ dround 2010-10-04 +1d 2010-11-01 
0
source

All Articles