プログラミングと日々思ったことなど

ブログ名通りです。仕事でプログラミングをはじめました。

日付について色々【VB.NET】

前回の記事から時間が経ってしまいました。
今回は、VB.NETでの日付変換についてです。

'和暦を日付変換することによって、正確な日付が入力されているのか確認する。
If Not DateTime.TryParse(cmb_年号.Text +
						 txt_年.Text + "年" +
						 txt_月.Text + "月" +
						 txt_日.Text + "日",chkDate) Then
	
	Call MsgBox("作成を入力してください",Me)
	cmb_年号.Focus()
	Return False

End If

chkDateの中には、多分こんなものの一部が入っています。
0730...chkDateは、何時何分何秒が入っているだけでした。このように和暦でも、TryParseはできるそうです。ただ、新しい和暦になるとエラーになってしまうかもしれないとのこと。(先輩談)

次は西暦から和暦の変換です。

Public Function chg_西和(ByVal str日付 As String,ByVal intフォーマット As Integer) As String

'基準日の年・月・日を取得
Dim BaseY As Integer
Dim BaseM As Integer
Dim BaseD As Integer

'ループに使う変数
Dim cnt As Integer
Dim 元号 As Integer = UBond(元号一覧) '元号一覧の中身も後で確認

'6桁なら(年月のみ)8桁にする
If str日付.Length = 6 Then
	str日付 = str日付 + "01"
ElseIf str日付 = "0" Then
	Return ""
End If

BaseY = Integer.Parse(Mid(str日付,1,4))
BaseM = Integer.Parse(Mid(str日付,5,2))
BaseD = Integer.Parse(Mid(str日付,7,2))

'戻り値初期化
 chg_西和 = ""

'元号の数だけループ
For cnt = 0 To 元号 Step 1

	With 元号一覧(Cnt)
	'取得した基準日と、DBにある元号開始日・終了日を比べる
	'開始日より基準日が大きい、かつ、基準日が終了日より小さい場合
	If(Integer.Parse(.getKaisi) <= Integer.Parse(str日付) And _
	   Integer.Parse(str日付) <= Integer.Parse(.getSyuryo)) Then

	   If intフォーマット = 0 Then
	   '平成xx年xx月xx日と返す
	   chg_西和 = .getGengoNm & _
	   			 String.Format("{0:D2}",BaseY - _
	   			 				(Integer.Parse(Mid(Trim(.getKaisi),1,4))-1)) &"年"& _
	   			 String.Format("{0:D2}",BaseM) &"月"& _
	   			 String.Format("{0:D2}",BaseD) &"日"&


	   Else If intフォーマット = 1 Then
	   '以下、戻す値の形が変更される処理が続く

	End If

Exit For

Else If (Integer.Parse(.getKaisi) > str日付 Or _
	   str日付 < Integer.Parse(str日付) >= Integer.Parse(.getSyuryo)) Then

	   MsgBox.Show("基準日が正しく設定されていません")
	   'For文を抜ける
	   Exit For
End If

End With
Next

End Function