LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-22-2012, 09:31 PM   #1
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Rep: Reputation: 15
last date of the previous month


What is the command to print last date of the previous month ?

Example: current month is April. So I need a command/script to print 31-March-2012

Do you have a solution ?

Last edited by Volcano; 04-22-2012 at 10:17 PM.
 
Old 04-22-2012, 11:00 PM   #2
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
This command will give you the last date of previous month-

Code:
# date -d "$(date -d "2012-04-23 -1 month" +%Y-%m-01) +1 month -1 day" +%Y-%m-%d
 
Old 04-22-2012, 11:15 PM   #3
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 7,286

Rep: Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384
I found this which seemed like it would work but actually goes back 2 months:

Quote:
date -d "-$(date +%d) days -1 month"
Wed Feb 29 21:10:18 MST 2012
Below worked:

Quote:
date -d "-$(date +%d) days"
Sat Mar 31 21:12:30 MST 2012
 
Old 04-23-2012, 12:12 AM   #4
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Satyaveer Arya View Post
This command will give you the last date of previous month-

Code:
# date -d "$(date -d "2012-04-23 -1 month" +%Y-%m-01) +1 month -1 day" +%Y-%m-%d
There should be no hardcoding as you put 23 april. Could you please correct it ?
 
Old 04-23-2012, 12:15 AM   #5
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by yancek View Post
I found this which seemed like it would work but actually goes back 2 months:



Below worked:

Your working code's output format is different than mine. Can you please correct it ?
 
Old 04-23-2012, 01:17 AM   #6
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
There should be no hardcoding as you put 23 april. Could you please correct it ?
Then you can put $today instead of today's date. like this-

Quote:
date -d "$(date -d "$today -1 month" +%Y-%m-01) +1 month -1 day" +%Y-%m-%d
And the output would be the same like this-
Quote:
2012-03-31
 
Old 04-23-2012, 01:40 AM   #7
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,487

Rep: Reputation: 390Reputation: 390Reputation: 390Reputation: 390
Quote:
Originally Posted by Volcano View Post
Your working code's output format is different than mine. Can you please correct it ?
Dude...do you want them to come over and type it in for you too?
 
Old 04-23-2012, 01:45 AM   #8
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Here is a change to get the output in this format - 20-March-2012.
Quote:
date -d "$(date -d "$today -1 month" +%Y-%m-01) +1 month -1 day" +%C-%B-%Y
Rest atleast you try something on your own to correct the command to get the correct date according to your need.
Most of the guys will not spoon feed you here.

Last edited by Satyaveer Arya; 04-23-2012 at 01:47 AM.
 
1 members found this post helpful.
Old 04-26-2012, 01:26 AM   #9
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Rep: Reputation: 0
Dear Satyaveer Arya,

Can you please explain us
Code:
date -d "$(date -d "$today -1 month" +%Y-%m-01) +1 month -1 day" +%C-%B-%Y
Thanks mate.
 
Old 04-26-2012, 04:39 AM   #10
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Hey Volcano,

I corrected the command and figured out that this command will print correctly last date of last month-
Code:
# date -d "$(date -d "$today -1 month" +%Y-%m-01) +1 month -1 day" +%d-%B-%Y
Output:
31-March-2012

Last edited by Satyaveer Arya; 04-26-2012 at 04:48 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Conversion : Month Name --> Number onesikgypo Programming 1 04-28-2009 11:26 AM
Setting to a previous date pwnd Linux - Newbie 5 07-13-2006 03:12 AM
Date command - month of year, blank padded? menator Programming 3 06-27-2006 06:00 AM
3 month's ago date's izza_azhar Programming 7 01-16-2005 10:53 PM
date command getting previous day swinchen Programming 6 08-23-2004 12:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration