2016-09-28 10 views
1

Ich brauche Hilfe zum Speichern von Datum und Uhrzeit in 1 Variable zum Speichern in SQL-Parametern.VB.NET Variable Datum und Uhrzeit

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

Und ich will "MM/dd/yyyy HH:mm:ss" in 1 Variable mit Format speichern.

dim CI as datetime 

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

Antwort

1

Obwohl ich nicht ganz ich verstehen, wie funktioniert diese Textfelder und die k Werke, empfehle ich Ihnen eine new DateTime(year, month, day, hour, minute, second) ein Datetime-Variable erstellen nutzen könnten.

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

Es gibt keine Konstruktorüberladung für 'DateTime', die eine 'Zeichenfolge' für Jahr, Monat und Tag enthält. – Enigmativity

+0

Entschuldigung, zu faul, um es zu übertragen und VB.net akzeptieren es trotzdem: P –

+2

VB akzeptiert nur, wenn Sie Option Strict nicht verwenden, was eine schlechte Praxis ist. –