|
-
Mar 5th, 2008, 08:38 AM
#1
Thread Starter
Fanatic Member
convert t odatetime
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
-
Mar 5th, 2008, 08:55 AM
#2
Re: convert t odatetime
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.
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
And you can call it like this:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(DateFromString("20080303123059"))
End Sub
-tg
-
Mar 5th, 2008, 08:59 AM
#3
Fanatic Member
Re: convert t odatetime
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
-
Mar 5th, 2008, 09:20 AM
#4
Re: convert t odatetime
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
-
Mar 5th, 2008, 09:28 AM
#5
Fanatic Member
Re: convert t odatetime
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.
-
Mar 5th, 2008, 09:30 AM
#6
Re: convert t odatetime
Im just curious, from where are you getting that string?
-
Mar 5th, 2008, 09:34 AM
#7
Thread Starter
Fanatic Member
Re: convert t odatetime
dear friend thanks a lot for reply
-
Mar 5th, 2008, 09:34 AM
#8
Fanatic Member
Re: convert t odatetime
to karthikeyan
 Originally Posted by Atheist
Im just curious, from where are you getting that string?
-
Mar 5th, 2008, 12:51 PM
#9
Re: convert t odatetime
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|