LoginSignup
4
8

More than 3 years have passed since last update.

UiPath 月初日・月末日取得方法(VB.NET)

Last updated at Posted at 2019-12-16

UiPath 月初日・月末日取得方法(VB.NET)

業務自動化でよく月初日や月末日を求めることが多いため自分用のメモ

変数を作成

変数名 変数の型
month_first_day System.DateTime
days Int32
month_last_day System.DateTime

上記の通り3つ変数を用意する。
追記の最後に記載している方法を使う場合はdaysはいらないです。

variablelist.png

System.DateTime型は 変数一覧 -> 変数の型 -> 型の参照で型の名前に入力すると出てくる。

datatype.png

変数の既定値を設定する

month_first_day

new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)

month_first_dayに上記を設定する。

DateTime.Now.Yearでシステム日付の年部分

DateTime.Now.Monthでシステム日付の月部分

最後の1で日付を月初にする。

month_first_day.png

days

DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)

daysに上記を設定する。

DaysInMonthは指定した年、月の日数を返します。(うるう年も計算する)

DateTime.Now.YearDateTime.Now.Monthを指定することでシステム日付の今月の日数を取得します。

days.png

month_last_day

new DateTime(DateTime.Now.Year, DateTime.Now.Month, days)

month_last_dayに上記を設定する。

DateTime.Now.Yearでシステム日付の年部分

DateTime.Now.Monthでシステム日付の月部分

daysで先ほど取得した今月の日数(最終日)を指定する。

month_last_day.png

テスト

Main.jpg

出力結果

debug.png

month_first_day.ToString("yyyy/MM/dd") '2019/12/01
month_first_day.ToString("yyyyMMdd") '20191201

ToStringの後で表示形式を変更できる。

※ 月部分はMMにするmmだと分になってしまう

参考サイト

追記

当月分のデータ取得するなどでタイムスタンプが同じようにyyyy/MM/dd hh:mm:ssのデータを比較する場合
month_first_dayはそのままでよいが、month_last_dayは以下のように設定する。

new DateTime(DateTime.Now.Year, DateTime.Now.Month, days).AddDays(1).AddSeconds(-1)

上記に設定後のmonth_last_day出力値(2019/12/17)

12/31/2019 23:59:59

これで、条件などに
month_first_day(12/01/2019 00:00:00) =< 比較データ >= month_last_day(12/31/2019 23:59:59)
と設定すれば当月分のデータを取得できる。

ここまで書いてて気づいたこと

month_last_day = month_first_day.AddMonths(1).AddDays(-1)

にすればdaysいらなかった。。。

4
8
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
8