LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2012, 05:07 PM   #1
em31amit
Member
 
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190

Rep: Reputation: 55
BASH ... Date /days subtraction Addition and compare


I am trying to create a bash shell script and as my requirement i need to subtract some days from current date and compare it to another date which is i am getting into last command.

But i am not getting any logic and a way to do it with in bash shell script. How can i implement it.
 
Old 05-23-2012, 05:12 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
What have you done so far?

Here's something on comparing dates in Bash:
http://stackoverflow.com/questions/5...date-variables
 
Old 05-23-2012, 06:53 PM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Calculations with dates always ends up with nasty code for the umpteenth exception you haven't thought about.

For internal calculations, convert dates to unix timestamps, using the date command. After you are done with your calculations, for presentation purposes, convert them back to human readable dates.

jlinkels
 
Old 05-24-2012, 12:06 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
GNU date is very useful for this:
Code:
date +%s -d "Fri May 11 12:34:56 2012"
date +%s -d "5 days ago"
date +%s
The output when using +%s is the number of seconds since the Epoch, so you can use $(( $(date +%s -d "later") - $(date +%s -d "earlier") )) in Bash to calculate the number of seconds between later and earlier.

Note that this will not work on all date variants; it will only work on GNU date, as far as I know.

To test if you have GNU date in Bash, you can use
Code:
if [ -n "date --version 2>/dev/null | grep -e GNU" ]; then
    # Yes, you have GNU 'date'
else
    # Oops, not a GNU 'date'!
fi
 
1 members found this post helpful.
Old 05-24-2012, 02:02 AM   #5
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
This tute shows that the "date" command has its own internal kind of arithmetic. http://linuxconfig.org/addition-and-...x-date-command An example:
Code:
$ date; date --date="5 hours ago" +%H%M-%d%m%Y
Mon Mar  7 09:54:42 EST 2011
0454-07032011
 
Old 05-24-2012, 01:12 PM   #6
em31amit
Member
 
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190

Original Poster
Rep: Reputation: 55
Thanks All, I'll try .
 
  


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
[SOLVED] how to get the days from a certain date? snowball0916 Linux - Newbie 4 12-12-2010 01:47 AM
[SOLVED] date subtraction using numbers tjay1983 Programming 11 11-18-2010 03:57 PM
Simple subtraction using bash and bc madpear Programming 2 11-04-2009 01:42 PM
date subtraction(Urgent) Uday123 Fedora 1 04-05-2006 03:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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