0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Bash】2期間の間の連続した日付のリストを返す

Posted at

関数

#!/bin/bash
set -o errexit # set -e
set -o nounset # set -u
set -o pipefail

# 2期間の間の日付の連番を返す
# Globals:
#   None
# Arguments:
#   begin YYYYMMDD
#   end YYYYMMDD
# Returns:
#   YYYYMMDD YYYYMMDD .. YYYYMMDD
function dateSeries() {
    local -r begin=$1
    local -r end=$2
    local -r begin_ut="$(date -d "${begin}" +%s)"
    local -r end_ut="$(date -d "${end}" +%s)"
    local -r interval="$(((end_ut - begin_ut)/(60*60*24)))"
    local dateList=()
    for days in $(seq 0 "${interval}"); do
        dateList+=("$(date +%Y%m%d -d "${begin} ${days}days")")
    done
    echo "${dateList[@]}"
}

# 配列として格納する場合
mapfile -t array < <(dateSeries 20250320 20250410)
echo "${array[@]}"
# 20250320 20250321 20250322 20250323 20250324 20250325 20250326 20250327 20250328 20250329 20250330 20250331 20250401 20250402 20250403 20250404 20250405 20250406 20250407 20250408 20250409 20250410

解説

dateコマンドの+%sでunix時間に直す(begin_ut, end_ut)。それを引き算し日数に変換して2期間の日数を求める(interval)。それをひとつづつ配列に格納する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?