473,287 Members | 3,181 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,287 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 48638
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.