前年の取得について

DateTime.Todayで今日の日時を変数「今日の日時」に入れた後、DateTime.Now.Date.ToString(“MM”)で今の月を変数「今の月」に取得しています。
同じような形で今の年を取得していますが、変数「今の月」が1~6月の場合は前の年を取得させるようにしたいです。
IFで「今の月== (“1-6”)」を条件にtrueならDateTime.Now.Year.Addyears(-1),ToString(“yy”)としましたが、前の年にならず、今の月が出てしまいます。
どのように記述したら良いでしょうか?

addmonthで-6してから年を取るってのは?

Are you really using DateTime.Now.Year.Addyears(-1),ToString(“yy”)? Because that should give you an error since you can’t do .Year.AddYears

Try DateTime.Now.AddYears(-1).ToString(“yy”) instead.

image

ありがとうございます。addmonth-6はどのように組み込むのでしょうか?
一年に渡って毎月ファイルを作成したいのですが、新たに変数にaddmonth-6したものを代入してから今の月を求めるという方法しか思い浮かびません…

また、上記で記載したif文の場合、今の月==(“5”)だと前の年の取得に成功するのですが、(“1-6”)以外に書き方があるのでしょうか?
よろしくお願いいたします。

ありがとうございます。私の記述ミスでした。他の方法なら前の年は取れていました!

Hi @sayaka.y

You write the expression like this
image

DateTime.Now.Month >= 1 AndAlso DateTime.Now.Month <= 6

Then, to get current month in MM format just use

DateTime.Now.Date.ToString("MM")

and to get last year just use

DateTime.Now.AddYears(-1).ToString("yy")

Regards!

now().addmonth(-6).year()
ですねー

こんにちは

以下の式で1-6月なら前年下2桁、7-12月なら当年下2桁を文字列でかえします。お試しください。

 (DateTime.Now.Year-2000-Convert.ToInt32(DateTime.Now.Month<=6)).ToString("00")

できました!ありがとうございました。

1 Like

試してみます。ありがとうございました!

いつもありがとうございます!
試してみます!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.