LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 06-16-2009, 01:31 AM   #1
myhnet
LQ Newbie
 
Registered: Jun 2008
Posts: 15

Rep: Reputation: 0
how to get the first and the last day in the month with "date" command


I am using CentOS 5.3 and I knew that we can easily to use "-d" option to get some special day.
like to get yesterday
Code:
date -d "yesterday"
to get today in the pasted mont
Code:
date -d "last month"
but is there a shortcut to get the day of the first and the last day in a month.

like
2009-02-01 2009-05-01 2009-06-01 (first day)
2009-02-28 2009-05-31 2009-06-30 (last day)


thank you very much

Last edited by myhnet; 06-16-2009 at 01:33 AM.
 
Old 06-16-2009, 01:44 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
As far as I know there is not a date specification for the last day of the month. To retrieve the last day of the current month I used the cal command:
Code:
echo $(cal) | awk '{print $NF}'
 
Old 06-16-2009, 01:50 AM   #3
sonnik
Member
 
Registered: May 2001
Posts: 149

Rep: Reputation: 17
Since you know the first day of the month is always the first, you can do the following if you'd like:

Code:
date -d "yesterday 2009-06-01"
Code:
date -d "2009-06-01"
The first line will provide "yesterday" relative to June 1.
 
Old 06-16-2009, 02:15 AM   #4
myhnet
LQ Newbie
 
Registered: Jun 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sonnik View Post
Since you know the first day of the month is always the first, you can do the following if you'd like:

Code:
date -d "yesterday 2009-06-01"
Code:
date -d "2009-06-01"
The first line will provide "yesterday" relative to June 1.
but you can not specify the month, like how you get this month?

Quote:
Originally Posted by colucix View Post
As far as I know there is not a date specification for the last day of the month. To retrieve the last day of the current month I used the cal command:
Code:
echo $(cal) | awk '{print $NF}'
seems a better choice but still complex.

Last edited by myhnet; 06-16-2009 at 02:18 AM.
 
Old 06-16-2009, 02:20 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Following the suggestion by sonnik, the following gives the last day of the month:
Code:
$ date -d "2009-06-01 yesterday 1 month"
Tue Jun 30 00:00:00 CEST 2009
$ date -d "2009-02-01 yesterday 1 month"
Sat Feb 28 00:00:00 CET 2009
 
Old 06-16-2009, 02:31 AM   #6
myhnet
LQ Newbie
 
Registered: Jun 2008
Posts: 15

Original Poster
Rep: Reputation: 0
but you still have a static date, how about you even don't know 2009-06-01
 
Old 06-16-2009, 02:31 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by colucix View Post
Code:
echo $(cal) | awk '{print $NF}'
why the echo?
Code:
cal |awk 'NF{last=$NF}END{print last}'

Last edited by ghostdog74; 06-16-2009 at 02:33 AM.
 
Old 06-16-2009, 02:50 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by myhnet View Post
but you still have a static date, how about you even don't know 2009-06-01
Code:
date -d "$(date +%Y-%m)-01 yesterday 1 month"
 
Old 06-16-2009, 02:56 AM   #9
myhnet
LQ Newbie
 
Registered: Jun 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Code:
date -d "$(date +%Y-%m)-01 yesterday 1 month"
hmmm, maybe this is the way.
thank you for your patience.
 
Old 06-16-2009, 03:16 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
make sure you test different combinations before implementing live. how to get the previous month's last day etc etc
 
  


Reply



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
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: A month on the command line -- Day 1 LXer Syndicated Linux News 3 05-06-2007 11:35 AM
"date" command and timezones estratos Linux - General 5 03-15-2007 03:47 PM
How to get the month in one digit instead of two digits in the "date" command? aadaileh Programming 7 02-05-2004 04:57 PM
Monthly Archiving Script... help with "date" & "cron" Supp0rtLinux Linux - Software 3 01-03-2003 09:29 PM

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

All times are GMT -5. The time now is 11:59 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
Open Source Consulting | Domain Registration