Adding one day to a date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding one day to a date
# 8  
Old 08-19-2015
Assuming a linux system, bash or ksh, and that that t_date already contains the last day of the month:
Code:
$ echo $t_date
20150531
$ date +%Y%m%d -d @$(expr 86400 + $(date +%s -d $t_date))
20150601

This works for leap years:
Code:
$ t_date=20160228
$ date +%Y%m%d -d @$(expr 86400 + $(date +%s -d $t_date))
20160229
$ t_date=20160229
$ date +%Y%m%d -d @$(expr 86400 + $(date +%s -d $t_date))
20160301

# 9  
Old 08-19-2015
And, if the OP doesn't have a Linux system, but does have a 1993 or later ksh, the following works correctly when $t_date expands to a YYYYMMDD representation of the last day of any month:
Code:
printf '%(%Y%m%d)T\n' "$t_date + 1 day"

And, we could all write code that will work in any POSIX conforming shell on any system if the submitter would only tell us what OS an shell is being used. But, I'm not going to waste my time doing that again until the submitter answers the simple questions I asked in post #2 in this thread:
Quote:
What operating system are you using?

Do you have access to a 1993 or later version of the Korn shell ( ksh93 or a ksh with a version later than 1993)?
so we can provide the help the submitter needs with a solution that will work in the submitter's environment.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies

2. Shell Programming and Scripting

Getting a Date value based on day

Hi, I have a scenario like this. I get a file on anyday of the week, so the file name is like this. FILE_NAME_YYYYMMDD I want to get the tuesday date before this day. For example FILE_NAME_20180413 I should get the date as 20180410 FILE_NAME_20180404 I should get the date as 20180403... (5 Replies)
Discussion started by: dnat
5 Replies

3. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

4. UNIX for Dummies Questions & Answers

Adding hours and minutes to current date (Only to date not to time)

Hi, I want to add some hours and minutes to the current date. For example, if the current date is "July 16, 2012 15:20", i want to add 5 hours 30 minutes to "July 16, 2012 00:00" not to "July 16, 2012 15:20". Please help. Thanks! (4 Replies)
Discussion started by: manojgarg
4 Replies

5. Shell Programming and Scripting

Adding days to system date then compare to a date

Hi! I am trying to read a file and every line has a specific date as one of its fields. I want to take that date and compare it to the date today plus 6 days. while read line do date=substr($line, $datepos, 8) #date is expected to be YYYYMMDD if ; then ...proceed commands ... (1 Reply)
Discussion started by: kokoro
1 Replies

6. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

7. UNIX for Dummies Questions & Answers

Getting date -1 day not using GNU date

It's easy as pie to get the date minus one day on opensolaris: date -d "-1 day" +"%Y%m%d"run this command on our crappy Solaris 10 machines however (which I'm guessing doesn't have GNU date running on it) and you get: date: illegal option -- d date: illegal option -- 1 date: illegal option --... (5 Replies)
Discussion started by: rich@ardz
5 Replies

8. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

9. UNIX for Dummies Questions & Answers

date - 1 day

Hi, I have been trying just about every unix command to come up with yesterday's date (today's date - 1). I have seen all of the help on this forum, and none of it seems to work for me here. We are using Sun Solaris 9 Unix. I am using this script to create a .txt file with ftp commands that I will... (2 Replies)
Discussion started by: sfedak
2 Replies

10. Shell Programming and Scripting

Getting day from a date...

Hi, I have a date input in MMDDYYYY format.. I have to give the day (whether that DD is sunday/monday...) Is there any command for it... Or do I have to write a script for that... Thanks in Advance Yeheya (1 Reply)
Discussion started by: yeheyaansari
1 Replies
Login or Register to Ask a Question