LoginSignup
0
0

More than 5 years have passed since last update.

onedaybeforethelastdayofthemonth

Posted at

cron

cronは自作してもっている。作ったのは7年ほど前で、コードはいま見ると目を覆うばかりなのだが、まあないよりマシ、動作しているのでそのまま使いつづけている。
それはそれとして自作のメリットは、拡張性の自由度がきわめて高いこと。
月末の1日前にリマインダーを起動する場合に、crontabに、こう書けるようにした。

19 20-23 onedaybeforethelastdayofthemonth * * reminder.exe

crontabの3項目めは日付を数字で入れるのだが、そこに27とか28とか29とか30とかと書くのでは月末の1日前にならない。
そこで、「onedaybeforethelastdayofthemonth」と書くと、それをcron実行時に、月末の1日前の数字に置き換えるようにした。
日付の数字はC#で次のようにして取得している。

var today = DateTime.Now;
var firstDayOfMonth = new DateTime(today.Year, today.Month, 1);
var oneDayBeforeTheLastDayOfMonth = 
    new DateTime(today.Year, today.Month, 
                 DateTime.DaysInMonth(today.Year, today.Month-1));
var lastDayOfMonth = 
    new DateTime(today.Year, today.Month, 
                 DateTime.DaysInMonth(today.Year, today.Month));
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