|
-
Apr 18th, 2021, 04:10 PM
#1
Thread Starter
Lively Member
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
-
Apr 18th, 2021, 04:18 PM
#2
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.
-
Apr 18th, 2021, 04:20 PM
#3
Thread Starter
Lively Member
Re: unable to change date format
 Originally Posted by OptionBase1
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
-
Apr 18th, 2021, 04:25 PM
#4
Thread Starter
Lively Member
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")
-
Apr 18th, 2021, 08:30 PM
#5
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
-
Apr 18th, 2021, 08:56 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|