2016-09-28 11 views
1

SQL変数に保存するには、日付と時刻を1変数に保存する必要があります。VB.NETの可変日付と時刻

I have textbox (txBulan) = "1", 
textbox (txTahun) = "2016", 
double (k) = "2", 
string (time) = "13:00:00" 

そして、私は1つの変数にフォーマット"MM/dd/yyyy HH:mm:ss"を保存したいと思います。

dim CI as datetime 

CI = CDate(Format((txBulan.Text & "/" & k-1 & "/" & CDbl(txTahun.Text) & " " & time), "MM/dd/yyyy HH:mm:ss")) 

答えて

1

私はかなりこれらのテキストボックスとk作品が行う方法を理解していないですが、私はあなたがDateTimeの変数を作成しnew DateTime(year, month, day, hour, minute, second)を使用することができますお勧めします。

Dim txYear as TextBox ' Assume Text = 2016 
Dim txMonth as TextBox ' Assume Text = 1 
Dim txDay as TextBox ' Assume Text = 2 
Dim time as String = "13:00:00" 
Dim Hour as Integer = -1 
Dim Minute as Integer = -1 
Dim Second as Integer = -1 

For Each piece as String in time.Split(":") 
    ' For safety, add try-catch 
    If (Hour < 0) Then 
     Hour = Integer.Parse(piece) 
    Else If (Minute < 0) Then 
     Minute = Integer.Parse(piece) 
    Else 
     Second = Integer.Parse(piece) 
    End If 
End For 

' For safety, cast .Text to integer value 
Dim Ci AS New DateTime(
    Integer.Parse(txYear.Text), 
    Integer.Parse(txMonth.Text), 
    Integer.Parse(tx.Day.Text), 
    Hour, Minute, Second 
) 
Dim DateString as String = Ci.ToString("MM/dd/yyyy HH:mm:ss") 
+0

年、月、日の文字列をとる「DateTime」のコンストラクタオーバーロードはありません。 – Enigmativity

+0

申し訳ありませんが、それをキャストするにはあまりにも怠惰ですVB.netはとにかくそれを受け入れる:P –

+2

VBはあなたが貧しい練習であるOption Strictを使用していない場合にのみ受け入れます。 –