LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-29-2013, 03:24 PM   #1
sakmr
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Rep: Reputation: Disabled
How to add days to specified date


Hi All,

My requirement is to add no.of days to specified date as shown below.

Please let me know how to convert the string to date format as "Tue 15 May 2012 17:22 IST" or similar and after conversion how to add no.of days to the date.

For Eg:

rechstring="201205151722"
adddays=5
output:201205201722
 
Old 03-29-2013, 03:55 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,961

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by sakmr View Post
Hi All,
My requirement is to add no.of days to specified date as shown below. Please let me know how to convert the string to date format as "Tue 15 May 2012 17:22 IST" or similar and after conversion how to add no.of days to the date.

For Eg:
rechstring="201205151722"
adddays=5
output:201205201722
Ok, so read the man pages on the date command, and pay attention to the "format" option. Google can also provide you with lots of options, if you look. What have you done/tried so far?
 
Old 03-29-2013, 04:24 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Another good starting point is the info utility: the date info pages are rich of examples and exahustive explanations. To dig into details about your question, see:
Code:
info --index-search="Relative items in date strings" date
 
1 members found this post helpful.
Old 03-30-2013, 11:21 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Just to be clear on it first, what environment are we talking about here? The shell, or another programming language?


Assuming the shell, as mentioned, the gnu version of the date command used by Linux is capable of directly modifying an input date, using the -d option. The only caveat is that the original date string mus be in a format it can understand first, such as the ISO standard "YYYY-MM-DD" (see info date -> date input formats).

Another option is to convert the date into epoch seconds, use simple arithmetic to add the appropriate number of seconds to it (e.g. +432000, for 5 days), and then reconvert it back into the desired format. But again, you'll probably have to use date or a similar program to convert to and from epoch.
 
Old 03-30-2013, 11:36 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
As David pointed out, you just need to go through manual of date command (see here).

In the meantime, following would work for you:
Code:
~$ date +%Y%m%d%H%M
 
  


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
BASH ... Date /days subtraction Addition and compare em31amit Programming 5 05-24-2012 02:12 PM
How to Manipulate Date based on number of days? pinga123 Solaris / OpenSolaris 1 03-02-2012 03:05 PM
[SOLVED] how to get the days from a certain date? snowball0916 Linux - Newbie 4 12-12-2010 02:47 AM
How do I calculate the number of days from a particular date using the <cal> utility? deepumnit Linux - Desktop 3 12-31-2007 01:12 PM
Deleting files after x days from the latest date file sriramsreedhars Programming 8 03-03-2007 10:30 AM


All times are GMT -5. The time now is 08:21 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration