LoginSignup
3
0

More than 3 years have passed since last update.

2020年2月29日の2年後は2022年何月何日?

Last updated at Posted at 2020-12-23

はじめに

現在、開発をしているプロジェクトがありまして、レコードの日付を2年ずらしてデータをずらしたことがありました。その時、たまたま 2020年2月29日 もその中に入っておりました。その時に得た結果をご披露します。この記事はソフトウェアテストの小ネタ Advent Calendar 2020 - Qiitaの25日目です。

MySQL

まず MySQL の場合は 2020年2月29日 の2年後は 2022年2月28日 になりました。

mysql> SELECT DATE_ADD( '2020-02-29', interval '2' YEAR ) ;
+---------------------------------------------+
| DATE_ADD( '2020-02-29', interval '2' YEAR ) |
+---------------------------------------------+
| 2022-02-28                                  |
+---------------------------------------------+
1 row in set (0.01 sec)
mysql>

逆に2020年2月29日 の2年前は 2018年2月28日 になりました。

mysql> SELECT DATE_ADD( '2020-02-29', interval '-2' YEAR ) ;
+----------------------------------------------+
| DATE_ADD( '2020-02-29', interval '-2' YEAR ) |
+----------------------------------------------+
| 2018-02-28                                   |
+----------------------------------------------+
1 row in set (0.00 sec)

mysql>

PostgreSQL

PostgreSQLの場合も MySQL と同様です。

postgres=# select cast('2020/2/29' as date) + cast('2 years' as INTERVAL);
      ?column?
---------------------
 2022-02-28 00:00:00
(1 row)

postgres=# select cast('2020/2/29' as date) + cast('-2 years' as INTERVAL);
      ?column?
---------------------
 2018-02-28 00:00:00
(1 row)

postgres=#

Excel

ところが、Microsoft Excelの場合は違います。
例えば、DATE関数を使用して、A1セルが2020年2月29日の時

=DATE(YEAR(A1)+2,MONTH(A1),DAY(A1))

2022年3月1日となります。なお、2年前は2018年3月1日となります。
ところが、

=EDATE(A1,24)

の場合は、MySQLと同様、 2020年2月29日 の2年後は 2022年2月28日 になります。2年前も同様に 2018年2月28日 になります。

おわりに

日付の処理の例外的パターンを調べてみました。使う処理系や関数によって結果が変わるので注意が必要だなと改めて思いました。

3
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
3
0