Need to get next valid date from date string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to get next valid date from date string
# 1  
Old 03-11-2010
Need to get next valid date from date string

Hi,

I am passing date string of format 'YYYYMMDD' to a ksh script.
Will I be able to get next valid date from the passed in string.

Code:
Example I pass '20100228' to the shell script, Is there a reverse date command to get '20100301' .i.e to convert '20100228' to date and get next date. Formatting can be done as required.

Pls help.
# 2  
Old 03-11-2010
what happens when you type
Code:
date -d 20100228

# 3  
Old 03-11-2010
Or if you are using ksh93 then you can use builtin properties:
Code:
#!/bin/ksh93
((daysec=24*60*60))
dstr="$1"
y=${dstr:0:4}
m=${dstr:4:2}
d=${dstr:6:2}

day=$(printf "%(%#)T"   "$y-$m-$d+00:00:00")
((prev=day-daysec))
printf "%(%Y%m%d)T  \n" "#$prev"
# or this enough in this case, no need for time
day=$(printf "%(%#)T"   "$y-$m-$d")
((next=day+daysec))
printf "%(%Y%m%d)T  \n" "#$next"

# 4  
Old 03-11-2010
You might want to search before posting. This exact question was asked just a few days ago.

Here is an alternate in bash for bsd, solaris, os x...

Look at the man page for date, and this logic can easily be changed to the differences on gnu or linux date.

Code:
#!/bin/bash

#call with deltaday 'date' 'deltadays'

#there is no error checking, use at your own use...

if [ ! -n "$1" ]
then
  echo "Usage: `basename $0` YYYYMMDD [+-days]"
  exit $E_BADARGS
fi  

if [ ! -n "$2" ]
then
  	deltadays=-1
  else
	deltadays=$2
fi  


epoch=$(date -j -f "%Y%m%d" $1 "+%s")

(( deltadays *= 60*60*24 ))
(( epoch += deltadays )) 

newday=$(date -j -f "%s" $epoch "+%Y%m%d")

echo $newday

# 5  
Old 03-11-2010
with gnu date:
Code:
$ date -d "tomorrow  20100228" +%Y%m%d
20100301

# 6  
Old 03-11-2010
Code:
date -d "+1 day   20100228" +%Y%m%d

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Valid separator in time and date format

Hello. I can use any particular (stupid or not) format when using bash date command. Example : ~> date --date "now" '+%Y-%m-%d %H!%M!%S' 2019-06-03 12!55!33or ~> date --date "now" '+%Y£%m£%d %H¤%M¤%S' 2019£06£03 12¤57¤36 or ~> date --date "now" '+%Y-%m-%d %H-%M-%S' 2019-06-03 12-58-51 ... (4 Replies)
Discussion started by: jcdole
4 Replies

2. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

3. Shell Programming and Scripting

Converting String Date into UNIX Date

Hi, I have a string date to my unix script(sun solaris). I wanted to convert it into unix date so that I can use it in a conditional statement. Please see below: MyTest.sh -s 2018-05-09 suppdt=$1 # string date passed via arguement as 2018-04-09 curryr=`date '+%Y'` nextyr=`expr... (2 Replies)
Discussion started by: Saanvi1
2 Replies

4. Shell Programming and Scripting

Compare Date with String in date format

Hi, I am new to this forum, searched, but for some reason did not find much help, so a new thread. I am trying to compare date with a string which is in date format , but for some reason, system does not give me the right result. Date is coming in a file and i am comparing the same with... (2 Replies)
Discussion started by: mkstool
2 Replies

5. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

6. Shell Programming and Scripting

Valid and invalid date in the file

Hi All, How to validate the 4th column,it is date column in the file, if it valid move to valid file else moved invalid file. 9f680174-cb87|20077337254|0|20120511|N 9f680174-cb88|20077337254|0|20120534|N i want two file valid.txt and invalid.txt Thanks, (7 Replies)
Discussion started by: bmk
7 Replies

7. Shell Programming and Scripting

Valid date checking in the file

File contian below data... 20111101 20111102 20111131 I am new to unix and per scirpt... First two records are valid and last record is invalid,how to get the valid records... Please help me unix or perl script. :wall: Thanks, Murali (5 Replies)
Discussion started by: muralikri
5 Replies

8. Homework & Coursework Questions

Get Julian date from date string

Hi, im new for UNIX. i have a problem in date function. please help me to find a solution. batchdate="29/10/2010" nextdate="01/11/2010" i want compare this two date. if my batch date greater than nextdate should prompt error message. how can i do that? as i know its better and safer if i... (2 Replies)
Discussion started by: ananth4mu
2 Replies

9. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies
Login or Register to Ask a Question