LoginSignup
4
6

More than 5 years have passed since last update.

dateコマンドをISO8601/RFC2822/RFC3339形式で出力する

Posted at

ISO8601

man date より抜粋

-I[TIMESPEC], --iso-8601[=TIMESPEC]
output date/time in ISO 8601 format. TIMESPEC='date' for date only (the default), 'hours', 'minutes', 'seconds', or 'ns' for date and time to
the indicated precision.

Sample Script

Gist上に同一スクリプト有。

iso_date.sh
#!/bin/bash


function date_wrapper() {
    command="date --iso-8601=$1"
    echo "$ ${command}"
    echo -e "`${command}`\n"
}


echo -e "date command output with ISO8601 format.\n"

command="date --iso-8601"
echo "$ ${command}"
echo -e "`${command}`\n"


for format in date hours minutes seconds ns
do
    date_wrapper ${format}
done
execution-example
> ./iso_date.sh
date command output with ISO8601 format.

$ date --iso-8601
2017-01-22

$ date --iso-8601=date
2017-01-22

$ date --iso-8601=hours
2017-01-22T14+0900

$ date --iso-8601=minutes
2017-01-22T14:01+0900

$ date --iso-8601=seconds
2017-01-22T14:01:16+0900

$ date --iso-8601=ns
2017-01-22T14:01:16,631109694+0900

RFC2822/RFC3339

man date より抜粋

-R, --rfc-2822
output date and time in RFC 2822 format. Example: Mon, 07 Aug 2006 12:34:56 -0600

--rfc-3339=TIMESPEC
output date and time in RFC 3339 format. TIMESPEC='date', 'seconds', or 'ns' for date and time to the indicated precision. Date and time com-
ponents are separated by a single space: 2006-08-07 12:34:56-06:00

Sample Script

Gist上に同一スクリプト有。

bash.sh
#!/bin/bash


function date_wrapper() {
    command="date --rfc-3339=$1"
    echo "$ ${command}"
    echo -e "`${command}`\n"
}


echo -e "--- date command output with RFC 2822 format ---"
command="date --rfc-2822"
echo "$ ${command}"
echo -e "`${command}`\n"


echo -e "--- date command output with RFC 3399 format ---"
for format in date seconds ns
do
    date_wrapper ${format}
done
execution-example
--- date command output with RFC 2822 format ---
$ date --rfc-2822
Sun, 22 Jan 2017 14:12:25 +0900

--- date command output with RFC 3399 format ---
$ date --rfc-3339=date
2017-01-22

$ date --rfc-3339=seconds
2017-01-22 14:12:25+09:00

$ date --rfc-3339=ns
2017-01-22 14:12:25.698220571+09:00
4
6
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
4
6