Visit Our UNIX and Linux User Community


Not able to fetch previous month first date and last date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Not able to fetch previous month first date and last date
# 1  
Old 04-13-2016
Not able to fetch previous month first date and last date

I am not able to fetch first date and last date previous month

Code:
date -d -1month +%Y-%m-%d 
date -d -1month +%Y-%m-%d

I need two format dd-mm-yyy
previous month 01-03-2016
previous month 31-03-2016
and also only date 1 to 31


Aprriciate your replay

Last edited by jagu; 04-13-2016 at 08:26 AM..
# 2  
Old 04-13-2016
While it's not that difficult to get at the last day of the last month:
Code:
date -d$(date +-%d)days
Do 31. Mär 13:50:08 CEST 2016

, calculating its first day is a bit more cumbersome. How about finding the solution in the FAQ area, e.g. Simple date and time calulation in BASH or Finding the nth Particular Week in a Month – shell script
# 3  
Old 04-13-2016
# 4  
Old 04-13-2016
HI Rudic,
It is diplsy correct output.please let me know if any other way to fetch for the same

Code:
 
set `date +%m" "%Y`
CURMTH=$1
CURYR=$2
if [ $CURMTH -eq 1 ]
then PRVMTH=12
     PRVYR=`expr $CURYR - 1`
 else PRVMTH=`expr $CURMTH - 1`
      PRVYR=$CURYR
 fi
if [ $PRVMTH -lt 10 ]
 then PRVMTH="0"$PRVMTH
fi
LASTDY=`cal $PRVMTH $PRVYR | egrep "28|29|30|31" |tail -1 |awk '{print $NF}'`
echo First Day: 01-$PRVMTH-$PRVYR
echo Last Day: $LASTDY-$PRVMTH-$PRVYR

Output-01-03-2016
31-03-2016
# 5  
Old 04-13-2016
Hi.

See date - First and last day of a month - Unix & Linux Stack Exchange for both simple and complex solutions with shell commands, some with GNU date, one without GNU date.

Best wishes ... cheers, drl

Previous Thread | Next Thread
Test Your Knowledge in Computers #795
Difficulty: Easy
10 has three prime factors 1, 2 and 5.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

2. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

3. UNIX for Dummies Questions & Answers

Get the previous month from date

Hi All, I am using the below code to get the year and month from date: Below gives output like 201212. dt=`date '+%Y%m'` how do i get the previous month value(ie: subtract 1 from date) example output: dt=201211 Please help. :confused: (3 Replies)
Discussion started by: abhi_123
3 Replies

4. Shell Programming and Scripting

Fetch date of 7 years back from current date in Perl

$beginDate = substr(DateCalc("today", "-7Days"),0,8); This fetches the date 7 days back Can I fetch the date before 7 years from todays date in Perl using same syntax Use code tags, see PM. (3 Replies)
Discussion started by: parthmittal2007
3 Replies

5. UNIX for Dummies Questions & Answers

date command - getting previous month

Hi, On any given day, I want to capture the month that has gone by - said otherwise, how do I capture last month? expr date '+%m' - 1 Above expression is giving error. Please advise thanks ---------- Post updated at 09:28 AM ---------- Previous update was at 09:11 AM... (1 Reply)
Discussion started by: ab_2010
1 Replies

6. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

7. Shell Programming and Scripting

Pass the first date and last date of previous month

Hi All, I need to run a job every month at the beginning of the month which is scheduled through autosys, lets say on 03/01/2010. I need to pass the last month's i.e February's first_date = 02/01/2010 and last_date = 02/28/2010 as variables to a stored procedure. Can somebody please pass... (2 Replies)
Discussion started by: vigdmab
2 Replies

8. UNIX for Dummies Questions & Answers

How to get the previous month by using `date`

Hello, I'm new to shell scripting. We've develop a script which will grep a file on the search criteria, MON (Jan/Feb/Mar/etc). But we should set this sript in cron which will run on every first day of the month. The problem I'm having is, when I run the script, it is displaying the contents of... (7 Replies)
Discussion started by: suneelj
7 Replies

9. Shell Programming and Scripting

needs to display month for previous day date

Hello, I wanted to display the month for previous day date. Like, today date is 18-Nov-2008. So the previous date is 17-Nov-2008. The output should be November. If the today date is 1-DEC-2008, then output should be NOVEMBER. If the today date is 1-JAN-2008, then output should be DECEMBER.... (4 Replies)
Discussion started by: govindts
4 Replies

10. Shell Programming and Scripting

Help, I need to get the last date of previous month

Hi, I'm new with Unix, I'm trying to get a last day of previous month with this format: %b %d %Y (example: Feb 25 2008). Here is what I have so far. #!/bin/ksh cur_month=`date +%m` cur_year=`date +%Y` prev_month=$(($cur_month-1)) # Check to see if this is January if then ... (11 Replies)
Discussion started by: sirrtuan
11 Replies

Featured Tech Videos