Dear Friend,
i have string as follows:
"20080303123059"
i need to convert this string as datetime. so please show me some example how to convert this as datetime
Printable View
Dear Friend,
i have string as follows:
"20080303123059"
i need to convert this string as datetime. so please show me some example how to convert this as datetime
Easy....
first, the left 4 is the year...
Then, if this follows the format I think it does... the month, followed by the day, hour, minute.... and looks like it goes to the second....
so what you will want to do is create a function that breaks the string up into it's components, re-assemble it into something formated to look like a date, then finally convert it to a date type and return it.
And you can call it like this:Code:Private Function DateFromString(ByVal DateIn As String) As Date
Dim newYear As String
Dim newMonth As String
Dim newDay As String
Dim newHour As String
Dim newMin As String
Dim newSec As String
newYear = DateIn.Substring(0, 4)
newMonth = DateIn.Substring(4, 2)
newDay = DateIn.Substring(6, 2)
newHour = DateIn.Substring(8, 2)
newMin = DateIn.Substring(10, 2)
newSec = DateIn.Substring(12, 2)
Dim newDate As String = newYear & "-" & newMonth & "-" & newDay & " " & newHour & ":" & newMin & ":" & newSec
Return CDate(newDate)
End Function
-tgCode:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(DateFromString("20080303123059"))
End Sub
vb Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim d As Long = 20080303123059 Dim str As String = String.Empty str = String.Format("{0:####/##/## 00:00:00}", d) MessageBox.Show(str) Dim dt As DateTime = CType(str, DateTime) MessageBox.Show(dt.Second) End Sub
d isn't a long..... it's a string.... but, it could be converted to a long, then sent through the .format....
tre cool...
-tg
true that d is a string, but I assumed that some conditions have already been applied to the string and then converted to a long to be able to fit my code.
Im just curious, from where are you getting that string?
dear friend thanks a lot for reply
to karthikeyan
Quote:
Originally Posted by Atheist
Here is just another way of doing it
vb.net Code:
Dim s As String = "20080303123059" Dim theDate As Date = Date.ParseExact(s, "yyyyMMddHHmmss", Nothing) MessageBox.Show(theDate.ToString)