Convert date format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chandru8
    New Member
    • Sep 2007
    • 145

    Convert date format

    I read the date from Notepad and convert it into date using Dateserial function. I try to convert it into my desired format like fomat( ,"dd/mm/yy") but I am unable to do that.
    [CODE=vb] Dim fs As New FileSystemObjec t
    Dim ts As TextStream
    Dim str As String
    Dim int1 As Integer
    Dim dt1 As String
    Dim dt As Date
    Dim date1 As Date
    Dim DATE2 As Date

    Set ts = fs.OpenTextFile ("c:\1.txt", ForReading, True)
    str = ts.ReadLine
    dt1 = Trim(Mid(str, 1, 10))
    date1 = DateSerial(Year (dt1), Month(dt1), Day(dt1))
    DATE2 = Format(date1, "mm/dd/yy")


    Set fs = Nothing
    ts.Close[/CODE]
  • shrimant
    New Member
    • Sep 2007
    • 48

    #2
    Code:
    Set ts = fs.OpenTextFile("c:\1.txt", ForReading, True)
    str = ts.ReadLine
    dt1 = Trim(Mid(str, 1, 10))
    date1 = Cdate(dt1)
    DATE2 = Format(date1, "mm/dd/yy")
    Serial is to convert a redable date to serial. see the code changes above..should work.

    Or else use

    FormatDateTime( dt1, <DateTimeEnumer ation>)

    DateTimeEnumera tion
    vbGeneralDate For real numbers, displays a date and time. If the number has no fractional part, displays only a date. If the number has no integer part, displays time only. Date and time display is determined by your computer's regional settings.
    vbLongDate Displays a date using the long-date format specified in your computer's regional settings.
    vbShortDate Displays a date using the short-date format specified in your computer's regional settings.
    vbLongTime Displays a time using the long-time format specified in your computer's regional settings.
    vbShortTime Displays a time using the short-time format specified in your computer's regional settings.

    Regards
    -Chanda Patel-
    [link removed]
    Last edited by MMcCarthy; Sep 27 '07, 08:08 AM. Reason: link removed - against site rules

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by chandru8
      I read the date ...
      I don't see how Notepad has anything to do with this.

      Anyway, allow me to try and explain the situation a bit. The Year(), Month() and Day() functions all accept a date value as a parameter, and extract the relevant piece of information from it. So you can't use them unless you already have a date value. What you have at that point is a string (which hopefully looks like a date). You need to use something like the DateValue() function to convert the string to a date.

      Conversely, the Format() function is used to format a value (such as a number or a date) into a string, generally for display purposes. Keep in mind that dates are not stored in any particular format. They are just stored as a number. You use things like Format() to convert the number to whatever format you want to see at the time.

      Try this version...

      [CODE=vb]Dim fs As New FileSystemObjec t
      Dim ts As TextStream
      Dim strng As String
      Dim dt1 As String
      Dim date1 As Date
      Dim Date2 As String

      Set ts = fs.OpenTextFile ("c:\1.txt", ForReading, True)
      strng = ts.ReadLine
      dt1 = Trim(Mid(strng, 1, 10))
      date1 = DateValue(dt1)
      Date2 = Format(date1,"m m/dd/yyyy")
      ' Note, it would be better to use something like this...
      Date2 = Format(date1,"s hort date")

      ts.Close
      Set ts = Nothing
      Set fs = Nothing
      [/CODE]Note that I changed the variable name str, since Str is a reserved word (it's the name of a function to convert a numnber to a string).

      Comment

      • chandru8
        New Member
        • Sep 2007
        • 145

        #4
        hi
        thanks for your reply

        i tried with both the codes, after getting the date value from the flat file i couldn't change to my desired format.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by chandru8
          I tried with both the codes, after getting the date value from the flat file i couldn't change to my desired format.
          Keep in mind that from VB's viewpoint what you are getting from the file is not a date value, but a string. To treat it as a date, you have to convert it.

          Can you show us what you have now, and explain what the trouble is with it? I mean, do you get an error, or just unexpected results, or what?

          Comment

          • chandru8
            New Member
            • Sep 2007
            • 145

            #6
            hi
            for this 27/09/07 iam getting 9/7/2027 ( 27/09/07 is from notepad)

            do you have any code also please send it its very urgent.

            Comment

            • QVeen72
              Recognized Expert Top Contributor
              • Oct 2006
              • 1445

              #7
              Hi

              How about this :

              Date2 = Format(cdate("2 7/09/07"),"dd-mm-yyyy")


              Date2 = Format(cdate(dt 1),"dd-mm-yyyy")

              REgards
              Veena

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by chandru8
                for this 27/09/07 iam getting 9/7/2027 ( 27/09/07 is from notepad)
                So, it's interpreting the value as being in either YY/MM/DD or YY/DD/MM format - there's no way to tell which from your example.

                Have you checked your date format in Windows Regional Settings? I mean, if you have a bunch of numbers with slashes between them, Windows has to take its best guess as to what is where. To me 9/7/2027 would mean the 9th of July, while to an American it would be the 7th of September.

                If all else fails, just chop up the string (Split() function can do this for you) into three parts, and feed the three pieces into the DateSerial() function. Since it has separate Year, Month and Day parameters, there's no way (hopefully) that it can misinterpret them.

                Comment

                Working...