CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Strange date

  1. #1
    Join Date
    Apr 2004
    Posts
    265

    Strange date-Resolved

    Hi,

    I have a strange problem with date.

    I retrieved a value from another application as string which is supposed to be a date. But when I change it to date, it turns upside down!

    For example, the value I retrieved is 20-09-04 where 20 is day, 09 is month and 04 is year. When I store it in a date variable, it turns to 9/4/2020. I even tried formatting it to "dd/mm/yy", but still the same. Please help.

    Thanks
    Last edited by shers; June 11th, 2006 at 12:11 AM.

  2. #2
    Join Date
    May 2006
    Location
    London (UK)
    Posts
    138

    Cool Re: Strange date

    Hi,

    You can use the following way :

    Code:
     
    Dim ToDate as Date
    ' FromDate is the variable which stores your Date String
     
    ToDate = Format(FromDate, "DD/MM/YYYY", vbSunday ,vbUseSystem)
     
    MsgBox Todate





    *

    Hope that Helps a Bit, but Sorry if it doesn't.
    Dont forget to Rate the Post...


    Thanks & Regards
    Manu Raj

    Nothings is Impossible in this World even Impossible says "I M possible"...

  3. #3
    Join Date
    Apr 2004
    Posts
    265

    Re: Strange date

    That didn't help. Now it displays as 4/9/2020

  4. #4
    Join Date
    May 2006
    Location
    London (UK)
    Posts
    138

    Arrow Re: Strange date

    Quote Originally Posted by shers
    That didn't help. Now it displays as 4/9/2020
    Please check ur System Date...




    *

    Hope that Helps a Bit, but Sorry if it doesn't.
    Dont forget to Rate the Post...


    Thanks & Regards
    Manu Raj

    Nothings is Impossible in this World even Impossible says "I M possible"...

  5. #5
    Join Date
    May 2006
    Location
    London (UK)
    Posts
    138

    Arrow Re: Strange date

    In my previous code try

    Code:
    Format(FromDate, "DD-MM-YYYY")

    *

    Hope that Helps a Bit, but Sorry if it doesn't.
    Dont forget to Rate the Post...


    Thanks & Regards
    Manu Raj

    Nothings is Impossible in this World even Impossible says "I M possible"...

  6. #6
    Join Date
    Jun 2006
    Posts
    437

    Re: Strange date

    Hi all,

    Try to use CDate to convert a string with a valid date format in a Date variable.
    Be carefull, CDate recognizes date formats according to the locale setting of your system; your date is in European format (day-month-year), and probably the locale setting of your system reads dates in American format; so, before to use CDate, you should change your string with the date in European format in a string whith the date in American format (it's quite easy to do).

    I hope this will help you.

  7. #7
    Join Date
    Aug 2000
    Posts
    242

    Re: Strange date

    A date string can be quiet tricky

    I strongly suggest that when ever you are using string to store date, use the YYYYMMDD date format this is the almost the only format in which you can know what is what.

    just be careful not to show this to the user

  8. #8
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Strange date

    I would agree with giladasaf about the format. I have used that to store dates where I needed to be able to sort them.

    If the date is always in DD-MM-YY format, what I would probably do is use Split() to form an array, then you can be certain each portion will be what you want. Then the DateSerial() function can format it properly every time.

    Something like this:
    Code:
    Dim MyDate() As String
    
    MyDate = Split(TheDate, "-")
    MsgBox DateSerial(MyDate(2), MyDate(1), MyDate(0))
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  9. #9
    Join Date
    Apr 2004
    Posts
    265

    Re: Strange date

    This is my code:

    DatePart = Split(strDate, "-")
    DayPart = DatePart(0)
    MonthPart = DatePart(1)
    YearPart = DatePart(2)
    checkDate = DateSerial(YearPart, MonthPart, DayPart)
    checkDate = Format(checkDate, "DD/MM/YY")

    where strDate = 20-09-04
    and the final checkDate gives 9/4/2020

  10. #10
    Join Date
    Dec 2004
    Posts
    423

    Re: Strange date

    Shers. When I try that that split function I get the right results.

    Try This :
    Code:
    Dim strWhat$
    Dim MyDate as Date
    Dim DartPart(0 to 2) as String
    
    DatePart = Split(strDate, "-")
    strwhat = DatePart(1) & "/" & DatePart(0) & "/" & DatePart(2)
    MyDate = CDATE(strwhat)
    Even if everybody spoke the same language, nobody would be speaking the same language.

    --Daniel

  11. #11
    Join Date
    Apr 2004
    Posts
    265

    Re: Strange date

    Sorry guys! I fixed it as I had to change the whole logic. Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)