Help with date in bash script for loop from YYYYMMDDHHMM

Hi everyone

I need some help

I want to create an script which does some processing

it takes the two arguments 201901010000 and 201901020200 - so YYYMMDDHHMM

I want to split processing into hours from start until end,

I dont get why this works but when I add to a future variable hours + minutes it stops work

d=$(date -d "20190308 + 1 hour" '+%Y%m%d%H%M'); echo $d            
201903080100
d=$(date -d "201903081000 + 1 hour" '+%Y%m%d%H%M'); echo $d 
date: invalid date `201903081000 + 1 hour'

All help is much appreciated, going to share also the script tomorrow morning, but I need to figure this one out

I don't know why it does not do that since it is all free format, possibly because it is not able to determine what part is the date and what constitutes time.

It does seem to work with a space before the time part, however:

date -d "20190308 1000 + 1 hour" '+%Y%m%d%H%M'

So to split the input you could for example try something like this:

$ datestr=201903081000
$ splitdatestr="${datestr%????} ${datestr#????????}"
$ printf "%s\n" "$datestr" "$splitdatestr"
201903081000
20190308 1000
1 Like