Results 1 to 9 of 9

Thread: convert t odatetime

  1. #1

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    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
    Loving dotnet

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: convert t odatetime

    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim d As Long = 20080303123059
    3.         Dim str As String = String.Empty
    4.  
    5.         str = String.Format("{0:####/##/## 00:00:00}", d)
    6.         MessageBox.Show(str)
    7.  
    8.         Dim dt As DateTime = CType(str, DateTime)
    9.  
    10.         MessageBox.Show(dt.Second)
    11.     End Sub

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    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.

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: convert t odatetime

    Im just curious, from where are you getting that string?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    Re: convert t odatetime

    dear friend thanks a lot for reply
    Loving dotnet

  8. #8
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: convert t odatetime

    to karthikeyan
    Quote Originally Posted by Atheist
    Im just curious, from where are you getting that string?

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: convert t odatetime

    Here is just another way of doing it
    vb.net Code:
    1. Dim s As String = "20080303123059"
    2. Dim theDate As Date = Date.ParseExact(s, "yyyyMMddHHmmss", Nothing)
    3. 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
  •  



Click Here to Expand Forum to Full Width