Results 1 to 9 of 9

Thread: Convert string in date

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    2,022

    Convert string in date

    i have mystring="31102014"
    how to convert in mydate=31/10/2014
    ????


    note:
    mystring as String
    mydate as Date

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,454

    Re: Convert string in date

    look up the format() function....

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    2,022

    Re: Convert string in date

    Quote Originally Posted by SamOscarBrown View Post
    look up the format() function....
    Me.LDAL.Caption = Format(mystring, "DD/MM/YYYY")

    but dont work!

  4. #4
    Addicted Member Wolfgang Enzinger's Avatar
    Join Date
    Apr 2014
    Location
    Munich, Germany
    Posts
    141

    Re: Convert string in date

    Quote Originally Posted by luca90 View Post
    i have mystring="31102014"
    how to convert in mydate=31/10/2014
    Code:
    Dim mystring As String
    Dim mydate As Date
    Dim y As Integer, m As Integer, d As Integer
    mystring = "31102014"
    d = CInt(Mid$(mystring, 1, 2))
    m = CInt(Mid$(mystring, 3, 2))
    y = CInt(Mid$(mystring, 5, 4))
    mydate = DateSerial(y, m, d)
    HTH, Wolfgang

  5. #5
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: Convert string in date

    Code:
    mystring = "31102014"
    mystring2 = Left$(mystring, 2) & "/" & Mid$(mystring, 3, 2) & "/" & Mid$(mystring, 5, 4)
    mydate = CVDate(mystring2)
    MsgBox mydate

  6. #6
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Convert string in date

    mystring = "31102014" will be no problem

    what do you think mystring = "11102014" will produce in the US , and What in EUR
    may i suggest you use "20141011" will work all over the world (YYYYMMDD)
    Last edited by IkkeEnGij; Dec 3rd, 2014 at 02:26 PM. Reason: added format

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,288

    Re: Convert string in date

    Using YYYYMMDD will also allow you to do a proper sort on the date value

  8. #8
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: Convert string in date

    Actually, CVdate is clever enough to sort out a few mismatches between regional formats. My little code snippet works on my system even if the string is in the US format and my computer is on International (European) setting (YYYYMMDD).

    But the year as 4 digits and the day as 31 makes it easy to find the mismatches, but I would not bet on "01/01/2014"

    To bet totally safe, only something like DateSerial would bring absolute peace of mind.

  9. #9
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: Convert string in date

    Quote Originally Posted by DataMiser View Post
    Using YYYYMMDD will also allow you to do a proper sort on the date value
    Yep, absolutely. That's old trick #1

    Old trick #2 is avoid am/pm at all costs... 24 hours format will sort dates and time in string format to the second (time unit).

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