Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print dates in date range linux

I am new to linux. How can I print and store date in given date range.

For example I have startdate=2013-03-01 and enddate = 2013-03-25 ; I want to print all date in that range.

Thanks in advance

like image 994
user1570210 Avatar asked Mar 25 '13 17:03

user1570210


People also ask

How do I echo date in Linux?

Sample shell script to display the current date and time #!/bin/bash now="$(date)" printf "Current date and time %s\n" "$now" now="$(date +'%d/%m/%Y')" printf "Current date in dd/mm/yyyy format %s\n" "$now" echo "Starting backup at $now, please wait..." # command to backup scripts goes here # ...

How do I get the current date in YYYY MM DD format in Linux?

To format date in YYYY-MM-DD format, use the command date +%F or printf "%(%F)T\n" $EPOCHSECONDS . The %F option is an alias for %Y-%m-%d . This format is the ISO 8601 format.

What is date in Linux command?

date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured. You must be the super-user (root) to change the date and time.


2 Answers

Another option is to use dateseq from dateutils (http://www.fresse.org/dateutils/#dateseq):

$ dateseq 2013-03-01 2013-03-25
2013-03-01
2013-03-02
2013-03-03
2013-03-04
2013-03-05
2013-03-06
2013-03-07
2013-03-08
2013-03-09
2013-03-10
2013-03-11
2013-03-12
2013-03-13
2013-03-14
2013-03-15
2013-03-16
2013-03-17
2013-03-18
2013-03-19
2013-03-20
2013-03-21
2013-03-22
2013-03-23
2013-03-24
2013-03-25
like image 200
nisetama Avatar answered Oct 27 '22 18:10

nisetama


As long as the dates are in YYYY-MM-DD format, you can compare them lexicographically, and let date do the calendar arithmetic without converting to seconds first:

startdate=2013-03-15
enddate=2013-04-14

curr="$startdate"
while true; do
    echo "$curr"
    [ "$curr" \< "$enddate" ] || break
    curr=$( date +%Y-%m-%d --date "$curr +1 day" )
done

With [ ... ], you need to escape the < to avoid confusion with the input redirection operator.

This does have the wart of printing the start date if it is greater than the end date.

like image 26
chepner Avatar answered Oct 27 '22 17:10

chepner