LoginSignup
4
4

More than 5 years have passed since last update.

jsで年度取得方法

Posted at

年度を表示する必要があったので moment を利用してかんたんにできたのでメモしておきます。

年度の仕組み

wiki参照すると

会計など事務作業を目的としたものが中心で、またその多くは政府機関や業界団体等により決定されている。日本における具体的な年度の例としては、4月1日から翌年3月31日までを括る「会計年度」や「学校年度」などが一般にも用いられる。この区切りは明治時代から続く。

要するに3月末までは前年扱いになります。

実装

momentを利用して3ヶ月前の日付を取得して年を取得するだけです。

const moment = require('moment');

moment('2016-12-31', 'YYYY-MM-DD').subtract(3, 'months').year();
// 2016
moment('2017-01-01', 'YYYY-MM-DD').subtract(3, 'months').year();
// 2016
moment('2017-03-31', 'YYYY-MM-DD').subtract(3, 'months').year();
// 2016
moment('2017-04-01', 'YYYY-MM-DD').subtract(3, 'months').year();
// 2017
4
4
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
4