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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-22-2012, 10:31 PM
|
#1
|
Member
Registered: Sep 2005
Posts: 225
Rep:
|
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 11:17 PM.
|
|
|
04-23-2012, 12:00 AM
|
#2
|
Senior Member
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
|
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
|
|
|
04-23-2012, 12:15 AM
|
#3
|
LQ Guru
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 11,049
|
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
|
|
|
|
04-23-2012, 01:12 AM
|
#4
|
Member
Registered: Sep 2005
Posts: 225
Original Poster
Rep:
|
Quote:
Originally Posted by Satyaveer Arya
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 ?
|
|
|
04-23-2012, 01:15 AM
|
#5
|
Member
Registered: Sep 2005
Posts: 225
Original Poster
Rep:
|
Quote:
Originally Posted by yancek
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 ?
|
|
|
04-23-2012, 02:17 AM
|
#6
|
Senior Member
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
|
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-
|
|
|
04-23-2012, 02:40 AM
|
#7
|
Senior Member
Registered: Mar 2012
Posts: 1,882
|
Quote:
Originally Posted by Volcano
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?
|
|
|
04-23-2012, 02:45 AM
|
#8
|
Senior Member
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
|
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 02:47 AM.
|
|
1 members found this post helpful.
|
04-26-2012, 02:26 AM
|
#9
|
Member
Registered: Mar 2010
Posts: 458
Rep:
|
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.
|
|
|
04-26-2012, 05:39 AM
|
#10
|
Senior Member
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
|
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 05:48 AM.
|
|
|
All times are GMT -5. The time now is 08:15 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|