LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to add days to specified date (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-add-days-to-specified-date-4175456057/)

sakmr 03-29-2013 02:24 PM

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

TB0ne 03-29-2013 02:55 PM

Quote:

Originally Posted by sakmr (Post 4921220)
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?

colucix 03-29-2013 03:24 PM

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

David the H. 03-30-2013 10:21 AM

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.

shivaa 03-30-2013 10:36 AM

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


All times are GMT -5. The time now is 11:03 AM.