CS3000911
CS3000911

Reputation: 131

Output format for dates in a range, with bash

I am trying to use bash to produce a list of dates and times between a starting point and an end point.

I would like the output to be in mm/dd/yyyy hh:mm format.

On the command line, the command:

date +"%m/%d/%Y %H:%M"

Produces the output that I am looking for.

When I use the line that is presently commented out, I get an error.

date: extra operand ‘%H:%M’
Try 'date --help' for more information.

I am not sure how to alter the script to produce the output that I am looking for.

DATE=date
#FORMAT="%m/%d/%Y %H:%M"
FORMAT="%m/%d/%Y"
start=`$DATE +$FORMAT -d "2013-05-06"`
end=`$DATE +$FORMAT -d "2013-09-16"`
now=$start
while [[ "$now" < "$end" ]] ; do
  now=`$DATE +$FORMAT -d "$now + 1 day"`
  echo "$now"
done

I have played around with adding an 00:00 after the start and end times, but that did not work.

Any ideas where I am getting the output format wrong?

Code from: https://ocroquette.wordpress.com/2013/04/21/how-to-generate-a-list-of-dates-from-the-shell-bash/

Upvotes: 0

Views: 67

Answers (1)

urban
urban

Reputation: 5682

When you use the FORMAT="%m/%d/%Y %H:%M" you need quotes because it contains a space, so:

now=`$DATE +"$FORMAT" -d "$now + 1 day"`

Also, I do not think that you can compare dates like that. You might need timestamp:

date +%s

Upvotes: 2

Related Questions