LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-03-2015, 02:41 AM   #16
KKaushal09
LQ Newbie
 
Registered: Feb 2015
Posts: 1

Rep: Reputation: Disabled
Thumbs up


Quote:
Originally Posted by xowl View Post
Hi, I found a faster way to do this.

I post them here cause it shows how powerful date is:

FirstDay
date -d "-1 month -$(($(date +%d)-1)) days"

Lastday
date -d "-$(date +%d) days -1 month"
Thanks xowl for such interesting answer . There is a correction here for LastDay of previous month:

OUTPUT for
date -d "-$(date +%d) days -1 month"(Lastday) will be the last day of previous to previous month.

So,

FirstDay previous month:
date -d "-1 month -$(($(date +%d)-1)) days"

Lastday previous month:
date -d "-$(date +%d) days"


FirstDay current month:
date -d "-$(($(date +%d)-1)) days"

Lastday current month:
date -d "-$(date +%d) days +1 month"
 
Old 12-14-2015, 12:54 AM   #17
root03
LQ Newbie
 
Registered: Dec 2015
Posts: 1

Rep: Reputation: Disabled
Answered by xowl himself, a little modification though

The answer is partly given by @XOWL himself. A little change in that syntax to meet 'DD-MM-YYYY' format :

Eg:
today's date = 14-12-2015
First day of last month = date '+%d-%m-%Y' -d "-1 month -$(($(date +%d)-1)) days" = 01-11-2015
Last day of last month = date '+%d-%m-%Y' -d "-$(date +%d) days" = 30-11-2015

eg 2 : Change of year: If the year changed and moved in to Jan, next year?
today's date (as per example) = 14-01-2016
First day of last month = date '+%d-%m-%Y' -d "-1 month -$(($(date +%d)-1)) days" = 01-12-2015
Last day of last month = date '+%d-%m-%Y' -d "-$(date +%d) days" = 31-12-2015

eg 3 : If the month changed over from Feb?
today's date (as per example) = 03-03-2016
First day of last month = date '+%d-%m-%Y' -d "-1 month -$(($(date +%d)-1)) days" = 01-02-2016
Last day of last month = date '+%d-%m-%Y' -d "-$(date +%d) days" = 29-02-2016
(checked for a leap year)


If format needs to be YYYY-MM-DD, then just change date '+%d-%m-%Y' to date '+%F'
 
Old 02-08-2017, 09:19 PM   #18
PhamTheThao
LQ Newbie
 
Registered: Dec 2014
Posts: 5

Rep: Reputation: Disabled
Hi everyone!
I have a bash script and I need it to check current day is 1st day of week or month or quarter or year or not?

This is my format date: #DATE=`date +%u-%d-%m-%Y_%Hh%Mm`

how to check it? thank you!
 
Old 02-09-2017, 09:49 AM   #19
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,830
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
Quote:
Originally Posted by PhamTheThao View Post
Hi everyone!
I have a bash script and I need it to check current day is 1st day of week or month or quarter or year or not?

This is my format date: #DATE=`date +%u-%d-%m-%Y_%Hh%Mm`

how to check it? thank you!
Please don't piggy back on old outdated (pun intended) threads. Open a new one instead. You'll get a wider audience on a new thread.
 
  


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
transform month number to month name in php ALInux Programming 1 11-09-2005 10:45 AM
Multicount in PHP , displays visits on current day, week, month, year and total visit xbaez Programming 1 04-24-2005 02:50 AM
bash month variable johniccp Linux - Newbie 1 12-06-2004 01:41 PM
cron last day of every month lobo78 Linux - General 2 03-03-2004 08:41 PM
Starting day of month, month length chrisk5527 Programming 2 03-03-2004 04:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:33 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