Date increment in the format "YYYYMMDD"

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date increment in the format "YYYYMMDD"
# 1  
Old 08-02-2006
Java Date increment in the format "YYYYMMDD"

Hi all,

I want to increment the date which is in the format "YYYYMMDD".
ex: If the date is 20010601

Increment should be: 20010602, 20010603 etc.,

Any help will be much appreciated.

Many Thanks and Regards,
# 2  
Old 08-02-2006

Id go with a perl-onliner like this

perl -ne '$foo=time + 86400;@bar=localtime($foo);printf("%d%02d%02d",$bar[5]+1900,$bar[4]+1,$bar[3])'

note the 86400 which is the amount of seconds for a day. The advantage of this is, you dont have to care if your month has 30 or 31 days.

alternativly you could use something like this

expr `date +%Y%m%d` + 1

But that will give you a wrong date at the last day of the month.

# 3  
Old 08-02-2006
Here is on other way:
$ cat dateincr
#! /bin/ksh

[[ $# != 1 ]] && echo "Enter a numeric value pls" && exit
#Taken from Vishu's script, to validate argument
[[ `echo $1 | grep [[:alpha:]]` || `echo $1 | grep [[:punct:]]` ]] && echo "$1 is not a number" && exit 1
let hours=$1*24
date1=`TZ=CST-$hours date +%Y%m%d`
echo "Here you go $date1"

$ ./dateincr 18
Here you go 20060820

# 4  
Old 08-02-2006
hello I am getting the error for || which is used in testcondition.

I am on HPUx.

also can any more throw some light on these extended regular expressions

# 5  
Old 08-02-2006
I'm not sure about HP-UX syntax, but you can skip validation part, only use the date code which you need in your script, this was a general date increment script, you can use something like this:
#! /bin/ksh
let hours=$1*24
date1=`TZ=CST-$hours date +%Y%m%d`
echo "Here you go $date1"

Remember this is ksh code, that matters too.

This User Gave Thanks to tayyabq8 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

awk "date" and "system" command

Hello experts! I need your help please I have a file.txt of which I want to extract 3rd and 4th columns with date with the form e.g.: 2016-11-25 03:14:50and pass them to "date" command, but also append the 9th column in a file as well. So I want to execute date -d '2016-11-25 03:14:50' ... (2 Replies)
Discussion started by: phaethon
2 Replies

3. Shell Programming and Scripting

How to increment date using "for loop" in format MMDDYY inside the shell script?

Need to increment the date from "currentdate + 90days" inside the for loop (i=1 to i=50) (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Format output from the file to extract "date" section

Hello Team , I have to extract date section from the below file output. The output of the file is as shown below. I have to extract the "" this section from the above output of the file. can anyone please let me know how can we acheive this? (4 Replies)
Discussion started by: coolguyamy
4 Replies

6. Shell Programming and Scripting

Can someone help me to convert the date format after get it from the "LAST REBOOT" command.

Can someone help me to convert the date format after get it from the "LAST REBOOT" command. these is the standard output. bash-3.00# last reboot reboot system boot Fri Aug 6 15:07 reboot system down Fri Aug 6 15:04 reboot system boot ... (3 Replies)
Discussion started by: pichitw
3 Replies

7. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2 I am trying to do an rsh grep to search for date records inside server logs by doing this : xx=`date +"%a %b %d"` rsh xxx grep "^$XX" zzz gives : grep: 0652-033 Cannot open Jun. grep: 0652-033 Cannot open 11. But if I do : xx=`date +"%a %b %d"` grep "^$XX" zzz it works... (2 Replies)
Discussion started by: Browser_ice
2 Replies

8. UNIX for Advanced & Expert Users

add seconds to: date"|"time"|"HHMMSS

Hey all, I have a shell that invokes a AWK. In this AWK i want invoke a function that receives 3 parameters: date: 20080831 time: 235901 duration: 00023 that function receive this 3 parameters and sum to this value two more seconds: 2008083123590100025 Remember that in case that... (3 Replies)
Discussion started by: anaconga
3 Replies

9. UNIX for Dummies Questions & Answers

converting date format: "May 31 2008" to "2008-05-31"

I have the following script to find out the last day of the last month .... and the output of this script is in the following format ... Script goes like this .... #!/bin/ksh cur_month=`date +%m` cur_year=`date +%Y` prev_month=$(($cur_month-1)) # Check to see if this is January if ... (8 Replies)
Discussion started by: santosham
8 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep ""|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep ""|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question

Featured Tech Videos