Convert "921124" to date?

C

cj2

What's the best way to convert "921224" to a date?

I could do:
dim mydatestring as new string = "921224"
dim mydate as date
mydate = mydatestring.substring(2,2) & "/" & mydatestring.substring(4,2)
& "/" & mydatestring.substring(0,2))

I reckon there is no better way but...

Oh and it's possible that would be formatted as YYYYMMDD too so I'd have
to check length before converting it.
 
J

James Hahn

The 6-character version won't work properly without an assumption for the
century.

dim mydatestring as new string = "921224"
....

If myDateString.Length = 6 Then myDateString = (Now.Year \ 100).ToString & D
Dim myDate As Date = New Date(myDateString.Substring(0, 4),
myDateString.Substring(4, 2), myDateString.Substring(6, 2))
 
F

Family Tree Mike

cj2 said:
What's the best way to convert "921224" to a date?

I could do:
dim mydatestring as new string = "921224"
dim mydate as date
mydate = mydatestring.substring(2,2) & "/" & mydatestring.substring(4,2) &
"/" & mydatestring.substring(0,2))

I reckon there is no better way but...

Oh and it's possible that would be formatted as YYYYMMDD too so I'd have
to check length before converting it.


Best in what terms?

In order to avoid needing to run with Option Strict Off, which your code
requires, I would prefer this:

Dim mydatestring As String = "921224"

Dim y As Integer = 1900 + Integer.Parse(mydatestring.Substring(0,
2)) ' I guess Y2K is non issue?
Dim d As Integer = Integer.Parse(mydatestring.Substring(4, 2))
Dim m As Integer = Integer.Parse(mydatestring.Substring(2, 2))

Dim mydate As New Date(y, m, d)

Your line setting the mydate to the result of a string is not wise, in my
opinion, and the reason you need Option Strict Off.

Your line call New String also does not compile.
 
F

Family Tree Mike

cj2 said:
What's the best way to convert "921224" to a date?

I could do:
dim mydatestring as new string = "921224"
dim mydate as date
mydate = mydatestring.substring(2,2) & "/" & mydatestring.substring(4,2) &
"/" & mydatestring.substring(0,2))

I reckon there is no better way but...

Oh and it's possible that would be formatted as YYYYMMDD too so I'd have
to check length before converting it.


Sorry, I missed the last question, but it would be done similar to the code
I just posted. You are correct, you check the length, but use the same
logic as before.
 
S

Stephany Young

The simplest way that I csn see is:

Dim _s As String "921224"

Dim _f As String = "yyyyMMdd"

Dim _date As DateTime = DateTime.ParseExact(_s, _f.SubString(_s.Length Mod
6, _s.Length), Nothing)
 
C

Cor Ligthert[MVP]

In my idea
\\\
Dim d921124 as DateTime
if "921124".Length = 6 then
d921124 = DateTime.ParseExact("921124", "yyMMdd", Nothing)
else
d921123 = DateTimeParseExact("19921123", "yyyyMMdd",Nothing)
end if
///
Although you can better use direct the constructor and then as sample only
with the 6 character length

\\\
dim d921124 as New DateTime("921124".Substring(0,2), 921124".Substring(2,2),
921124".Substring(4,2))
///

By the way dim mydatestring as new string is not the same as dim
mydatestring as string

You can use the function string to construct an array of characters.

Cor
 
C

cj2

Thanks Cor. datetime.parseexact("921124","yyMMdd",Nothing) is exactly
what I was looking for. Understandable and short.
 
C

cj2

Guess Y2K was too long ago now. the assumption of year hadn't crossed
my mind yet. I guess it hadn't crossed my mind because VB makes the
correct assumption in this case that it's 1992. Which makes me wonder
at what year does VB starts assuming it's 20?? instead of 19??

P.S. I have no control over the date string I receive. It will either
be YYMMDD or CCYYMMDD.
 
C

cj2

Apparently VB takes 20 as 2029 but 30 as 1930.

? datetime.parseexact("291124","yyMMdd",Nothing)
#11/24/2029#
? datetime.parseexact("301124","yyMMdd",Nothing)
#11/24/1930#

Don’t know if that is based on those years or distance from this year.
I’m going with fixing it myself 1940 and 2039 based on our needs.
 
M

Mythran

cj2 said:
What's the best way to convert "921224" to a date?

I could do:
dim mydatestring as new string = "921224"
dim mydate as date
mydate = mydatestring.substring(2,2) & "/" & mydatestring.substring(4,2) &
"/" & mydatestring.substring(0,2))

I reckon there is no better way but...

Oh and it's possible that would be formatted as YYYYMMDD too so I'd have
to check length before converting it.


Weee, surprised nobody mentioned TryParseExact. You don't need to check a
string length. You can use TryParseExact and pass in an array of formats to
the formats parameter which will test against each format until it finds a
match. If the input does not match any of the formats, the method returns
false. If the input does match one of the formats, the method will set the
output parameter to the parsed Date value and return True.

Example:

Dim formats As String() = { "yyMMdd", "yyyyMMdd" }
Dim input As String = "921124"
Dim output As Date

Dim provider As System.Globalization.DateTimeFormatInfo = _
System.Globalization.DateTimeFormatInfo.CurrentInfo
Dim style As System.Globalization.DateTimeStyles = _
System.Globalization.DateTimeStyles.None

If Not DateTime.TryParseExact(input, formats, provider, style, output)
Console.WriteLine("The input is not in a valid format.")
Else
Console.WriteLine(output.ToString("MM/dd/yyyy"))
End If

HTH :)

Mythran
 
C

cj2

That's a good suggestion but I'm sticking with Cor's example as I'm
going to append my own century onto 6 digit dates and I find adjusting
VB's default century settings not as clear to the reader how things are
being done as having it right there in code.
 
M

Mythran

cj2 said:
That's a good suggestion but I'm sticking with Cor's example as I'm going
to append my own century onto 6 digit dates and I find adjusting VB's
default century settings not as clear to the reader how things are being
done as having it right there in code.

cj2,

Doing it yourself is fine, but watch out. The .Net methods for parsing
dates are standardized and when they are updated, you'll lose out on the
updates because you are parsing your own dates to add your own century. You
can reflect the method to figure out how it's adding the century based on
the data provided:

The internal code actually calls another public method of the Calendar class
called ToFourDigitYear. This method

Microsoft created a variable which can be updated that specifies a
four-digit year where any two-digit year greater than the four-digit year
modulo 100 (so the last two digits in the four-digit year) would be set to
last century, while anything equal to or before it would be this century.
This four-digit year is set on Windows machines in Control Panel under
"Regional and Language Options"->"Regional Options" tab->"Standards and
formats" group box button "Customize"->Date tab->Calendar group box.

To sum this up, using the methods provided by the Microsoft through the .Net
Framework would probably be your best bet. The code isn't really that
obscure or not clear to developers...just a little digging and we are able
to see exactly what's going on in the Framework and if you really need to
show this to the user, then you can using the dialogs provided in the
Control Panel for Regional and Language Options....using your own way of
doing this would be reinventing the wheel...but in the end, it is up to you.

HTH,
Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top