Results 1 to 6 of 6

Thread: unable to change date format

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    71

    unable to change date format

    I am trying to convert a date into another format.

    Code:
               FixDate = "17 JULI 2018"
                Dim newDate As Date = DateTime.ParseExact(FixDate, "YYYYMMDD", Globalization.CultureInfo.InvariantCulture)
                finalstring = newDate.ToString
    I get an error saying the date format is not recognized.

    The result should be a date in string like this: 20180717

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: unable to change date format

    Have you consulted the documentation for DateTime.ParseExact?

    https://docs.microsoft.com/en-us/dot...t?view=net-5.0

    The second parameter needs to be a string that specifies the format of the contents of the first parameter. You seem to think that the second parameter is some sort of "how I want the resulting date to be formatted" parameter, which is incorrect and is also flawed reasoning when understanding how a Date variable works.

    Good luck.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    71

    Re: unable to change date format

    Quote Originally Posted by OptionBase1 View Post
    Have you consulted the documentation for DateTime.ParseExact?

    https://docs.microsoft.com/en-us/dot...t?view=net-5.0

    The second parameter needs to be a string that specifies the format of the contents of the first parameter. You seem to think that the second parameter is some sort of "how I want the resulting date to be formatted" parameter, which is incorrect and is also flawed reasoning when understanding how a Date variable works.

    Good luck.
    That is exactly what I was thinking

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    71

    Re: unable to change date format

    In VBA this used to work. Why doesn't it in VB.NET?

    Code:
    munck = CDate(FixDate)
    finalstring = Format(munck, "YYYYMMDD")

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: unable to change date format

    To OptionBase's point, YYYYMMDD does not specify the format of 17 JULI 2018. What you actually want is:
    Code:
    Dim FixDate = "17 JULY 2018"
    Dim newDate = DateTime.ParseExact(FixDate, "dd MMMM yyyy", Globalization.CultureInfo.InvariantCulture)
    Dim finalString = newDate.ToString("yyyyMMdd")
    Example: https://dotnetfiddle.net/787qra
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: unable to change date format

    The ParseExact method converts FROM a String to a DateTime, so what use is specifying the format you want to convert TO? You have to specify the format you want to convert FROM. Once you have a DateTime, you then need to convert that to a String in the new format, so THAT is the step at which you have to specify the format you want to convert TO.

    You need to understands that DateTime values HAVE NO FORMAT. Format is only a concern when representing that value as text. If you want to convert from text in one format to text in another format then you have to go via a DateTime that has no format. It is two steps and you have to specify the appropriate text format in each step.

Tags for this Thread

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