LinuxQuestions.org
Review your favorite Linux distribution.
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 03-26-2018, 08:26 PM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 423

Rep: Reputation: 0
How to read date and convert it into yesterday's date ?


Doing something like this but damn in it is not working

Code:
export DATE=2017-11-01
yest=$(date --${DATE}="yesterday" +"%Y-%m-%d")
date: unrecognized option '--2017-11-01=yesterday'
Try `date --help' for more information.

yest=$(date --$"{DATE}"="yesterday" +"%Y-%m-%d")
date: unrecognized option '--{DATE}=yesterday'
Try `date --help' for more information.

Basically, I want to read user input, they can input any date, bash would have a variable called previous_date which would compute given input's date to yesterday's date

Would greatly appreciate your assistance
 
Old 03-26-2018, 08:37 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,002

Rep: Reputation: 3288Reputation: 3288Reputation: 3288Reputation: 3288Reputation: 3288Reputation: 3288Reputation: 3288Reputation: 3288Reputation: 3288Reputation: 3288Reputation: 3288
The option is the literal string "--date" - which can be assigned a value such as ${DATE} or "yesterday". A value - singular.

Edit: - knew I had something somewhere. See if you can find dateutils - you might like some of the included tools.

Last edited by syg00; 03-26-2018 at 08:39 PM.
 
1 members found this post helpful.
Old 03-26-2018, 09:10 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,248

Rep: Reputation: 3475Reputation: 3475Reputation: 3475Reputation: 3475Reputation: 3475Reputation: 3475Reputation: 3475Reputation: 3475Reputation: 3475Reputation: 3475Reputation: 3475
In addition, the date man page sort of explains the date string syntax. As stated --date is an option not a variable.

Code:
date --date="string" +"output format"
One example of finding yesterdays date using a variable is:

Code:
DATE=2017-11-01
yest=$(date --date="$DATE yesterday" +"%Y-%m-%d")
 
1 members found this post helpful.
Old 03-26-2018, 09:13 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,431

Rep: Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030Reputation: 2030
Code:
DATE=7/27/1952
date -d "$DATE - 1 day" +"%Y-%m-%d"
 
1 members found this post helpful.
Old 03-27-2018, 07:44 AM   #5
sysmicuser
Member
 
Registered: Mar 2010
Posts: 423

Original Poster
Rep: Reputation: 0
Many thanks to all of you. I shall always be indebted to this forum for teaching me a great deal
 
Old 05-03-2018, 11:28 PM   #6
hroptatyr
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Rep: Reputation: Disabled
The dateutils solution:

Code:
DATE=2018-03-20
dateadd $DATE -1d
=>
  2018-03-19
 
  


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
[SOLVED] Convert Julian date to Calendar date keif Programming 3 03-21-2014 09:16 AM
Yesterday's date in unix priyophan Linux - Server 10 03-31-2011 05:33 PM
Need variable for yesterday's date stefaandk Programming 8 01-28-2007 10:50 PM
current date - yesterday's date newbie_adm Linux - Newbie 4 09-04-2006 03:56 PM

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

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