473,387 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Type Conversion with Date formatted as YYYYMMDD

I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"

If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)

How can I perform a type conversion for the date format YYYYMMDD?

Thanks.
Mar 14 '07 #1
10 48659
On Mar 14, 1:08 pm, Mike <M...@discussions.microsoft.comwrote:
I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"

If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)

How can I perform a type conversion for the date format YYYYMMDD?

Thanks.
Private Function ConvertDate(ByVal value As String) As Date

' Converts a date in YYYYMMDD format to MM/DD/YYYY format.

If Not value.Length = 8 Then
Throw New ArgumentException("Invalid length")
End If

If Not IsNumeric(value) Then
Throw New ArgumentException("Not a valid 8-digit number.")
End If

Dim theYear As String = value.SubString(0, 4)
Dim theMonth As String = value.SubString(4, 2)
Dim theDay As String = value.SubString(6, 2)
Dim result As String = theMonth & "/" & theDay & "/" & theYear

If Not IsDate(result) Then
Throw New ArgumentException("value cannot be converted to a
valid date.")
End If

Return CDate(result)

End Function

Basically, parse it into it's constituent parts, then splice it back
together into the format you want. I'm sure there's a regular
expression you could use to do it, but I'm all for clear code that
maintenance programmers can read. Plus, I get to add lots of error
checking that validates the argument.

Hope this helps.

Mike

Mar 14 '07 #2
Absolutely Perfect! Thank you so very much for your assistance, Mike.

Mike

"Mike Hofer" wrote:
On Mar 14, 1:08 pm, Mike <M...@discussions.microsoft.comwrote:
I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"

If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)

How can I perform a type conversion for the date format YYYYMMDD?

Thanks.

Private Function ConvertDate(ByVal value As String) As Date

' Converts a date in YYYYMMDD format to MM/DD/YYYY format.

If Not value.Length = 8 Then
Throw New ArgumentException("Invalid length")
End If

If Not IsNumeric(value) Then
Throw New ArgumentException("Not a valid 8-digit number.")
End If

Dim theYear As String = value.SubString(0, 4)
Dim theMonth As String = value.SubString(4, 2)
Dim theDay As String = value.SubString(6, 2)
Dim result As String = theMonth & "/" & theDay & "/" & theYear

If Not IsDate(result) Then
Throw New ArgumentException("value cannot be converted to a
valid date.")
End If

Return CDate(result)

End Function

Basically, parse it into it's constituent parts, then splice it back
together into the format you want. I'm sure there's a regular
expression you could use to do it, but I'm all for clear code that
maintenance programmers can read. Plus, I get to add lots of error
checking that validates the argument.

Hope this helps.

Mike

Mar 14 '07 #3
Mike wrote:
I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"

If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)
<snip>

I guess you'll be glad to know that the Date class has a Shared method
to do just that:

Dim D As Date = Date.ParseExact(X, "yyyyMMdd", Nothing)

HTH.

Regards,

Branco.

Mar 14 '07 #4
Branco, Perfect! Thank you!

"Branco Medeiros" wrote:
Mike wrote:
I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"

If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)
<snip>

I guess you'll be glad to know that the Date class has a Shared method
to do just that:

Dim D As Date = Date.ParseExact(X, "yyyyMMdd", Nothing)

HTH.

Regards,

Branco.

Mar 14 '07 #5
Mike,

Why not use the constructor.

dim myDateString as string = CStr(New DateTime(2000, 1, 1).Date)

Cor

"Mike" <Mi**@discussions.microsoft.comschreef in bericht
news:8E**********************************@microsof t.com...
>I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"

If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)

How can I perform a type conversion for the date format YYYYMMDD?

Thanks.

Mar 15 '07 #6
On Mar 14, 3:58 pm, "Branco Medeiros" <branco.medei...@gmail.com>
wrote:
Mike wrote:
I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"
If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)

<snip>

I guess you'll be glad to know that the Date class has a Shared method
to do just that:

Dim D As Date = Date.ParseExact(X, "yyyyMMdd", Nothing)

HTH.

Regards,

Branco.
It never ceases to amaze me the kinds of stuff that I overlook in the
Framework. I use the DateTime class all the time. I don't know how
many times I've manually coded that solution, and didn't need to. I
should have figured there'd be a Framework method for handling it. I
just didn't look in the right place for it.

Thanks for pointing that out, Bronco.
Mar 15 '07 #7
On Mar 15, 2:01 am, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
wrote:
Mike,

Why not use the constructor.

dim myDateString as string = CStr(New DateTime(2000, 1, 1).Date)

Cor

"Mike" <M...@discussions.microsoft.comschreef in berichtnews:8E**********************************@m icrosoft.com...
I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"
If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)
How can I perform a type conversion for the date format YYYYMMDD?
Thanks.- Hide quoted text -

- Show quoted text -
He can't use the constructor for it, because the numbers are glommed
together in an 8-character string, and he has to parse them out.
Hence, Branco's suggestion of using the ParseExact method, which seems
eminently suited to the task.

Mar 15 '07 #8
Mike Hofer wrote:
<snip>
It never ceases to amaze me the kinds of stuff that I overlook in the
Framework. I use the DateTime class all the time. I don't know how
many times I've manually coded that solution, and didn't need to. I
should have figured there'd be a Framework method for handling it. I
just didn't look in the right place for it.
Tell me about it... This happens to me all the time. While it's great
to find a clever solution to a given problem by mining a ready-made
piece of code from the framework, some times it's just as frustrating
to dive into the framework and come out empty handed... just to
discover later that you were *this close* to the solution, but failed
to see it (or click in the correct MSDN help topic, actually).

I saw this happen just a few days ago, in another thread, while
looking up ways to discover schema information from the .Net data
providers (something that really interests me). The class that
actually provided the information was right there and I missed it
(despite some sort of intuition that kept telling me "look in the
datareader... look in the datareader..."). Fortunately Robin (a nod to
you, king) came out with the appropriate solution... ;-)
Thanks for pointing that out, Bronco.
Ops! "Bronco" (with an "o" in the 3rd letter) in portuguese means
"dumb". Thanks for pointing *that* out... =))))

Regards,

Branco.

Mar 15 '07 #9
On Mar 15, 12:01 pm, "Branco Medeiros" <branco.medei...@gmail.com>
wrote:
Mike Hofer wrote:

<snip>
It never ceases to amaze me the kinds of stuff that I overlook in the
Framework. I use the DateTime class all the time. I don't know how
many times I've manually coded that solution, and didn't need to. I
should have figured there'd be a Framework method for handling it. I
just didn't look in the right place for it.

Tell me about it... This happens to me all the time. While it's great
to find a clever solution to a given problem by mining a ready-made
piece of code from the framework, some times it's just as frustrating
to dive into the framework and come out empty handed... just to
discover later that you were *this close* to the solution, but failed
to see it (or click in the correct MSDN help topic, actually).

I saw this happen just a few days ago, in another thread, while
looking up ways to discover schema information from the .Net data
providers (something that really interests me). The class that
actually provided the information was right there and I missed it
(despite some sort of intuition that kept telling me "look in the
datareader... look in the datareader..."). Fortunately Robin (a nod to
you, king) came out with the appropriate solution... ;-)
Thanks for pointing that out, Bronco.

Ops! "Bronco" (with an "o" in the 3rd letter) in portuguese means
"dumb". Thanks for pointing *that* out... =))))

Regards,

Branco.
DOH!

<slams head on desk>

Can I step in a big heapin' pile o' doggy doo or what?! :-D

Mar 15 '07 #10

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11*********************@p15g2000hsd.googlegro ups.com...
Mike Hofer wrote:
<snip>
>It never ceases to amaze me the kinds of stuff that I overlook in the
Framework. I use the DateTime class all the time. I don't know how
many times I've manually coded that solution, and didn't need to. I
should have figured there'd be a Framework method for handling it. I
just didn't look in the right place for it.

Tell me about it... This happens to me all the time. While it's great
to find a clever solution to a given problem by mining a ready-made
piece of code from the framework, some times it's just as frustrating
to dive into the framework and come out empty handed... just to
discover later that you were *this close* to the solution, but failed
to see it (or click in the correct MSDN help topic, actually).

I saw this happen just a few days ago, in another thread, while
looking up ways to discover schema information from the .Net data
providers (something that really interests me). The class that
actually provided the information was right there and I missed it
(despite some sort of intuition that kept telling me "look in the
datareader... look in the datareader..."). Fortunately Robin (a nod to
you, king) came out with the appropriate solution... ;-)
>Thanks for pointing that out, Bronco.

Ops! "Bronco" (with an "o" in the 3rd letter) in portuguese means
"dumb". Thanks for pointing *that* out... =))))

Regards,

Branco.
Thanks, Branco. I'm glad I posted something that was helpful to you!

Robin S.
Mar 16 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
7
by: Alphonse Giambrone | last post by:
How can I convert a string to a different type based on another string or other variable? For instance, instead of Dim i as Integer i = ctype("1000", Integer) I would like to do
0
by: Ulrich Wisser | last post by:
Hi, today I started to wonder about type conversion. I want to get all rows of a table dated between two given dates. Until now I use select * from mytable where to_date('20040115',...
10
by: Arno R | last post by:
Hi all, I have a database that I need to use in different versions of Access. This is A97 in most places and A2k in a few other locations. (I develop in A97 and convert the db to A2k for these...
2
by: john in fl | last post by:
Hello Everyone, I have an Append Query that worked fine in Access97 but now fails to work in Access 2000. The error that occurs is that it puts null values in a field due to a "Type Conversion...
6
isben22
by: isben22 | last post by:
I am a beginning developer using ACCESS 03 and Share Point Portal 2003. I have a list of events with start and stop dates in SharePoint that I want to pass to and Access Data Base. Ultimatly I...
1
by: jclover | last post by:
I'm using Microsoft Access to try to put together a form that enters data into a table when the form is closed, not a typical live form. Several fields are combo lists that pull from various...
4
by: smugcool | last post by:
HI, I am trying to import an excel data. All the fields are geting imported properly. But i am geting error in the date field. I tried to keep the format both in excel and access very similar.But...
6
by: GoNowhere | last post by:
Hi There, I'm attempting to update a short date formatted field with a consistent year without changing the Month or Day. Below is what I have so far, when trying to update it however I get a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.