how to convert 161009 to a date, and 1356 to a time

  • Thread starter Thread starter Aussie Rules
  • Start date Start date
A

Aussie Rules

Hi,

I have a file from an 'older' system that has two text fields, one contains
the date, the other the time.

In the date field, I get for example the value 161009. The first 2 are day,
the next 2 are month, and the last 2 are year.

To get it into a date format that I can use, I tried the following (actdate
is a date type)

actdate = tday & "/" & tmonth & "/" & tyear

But this fails....

I have to do the same with the time (1356 = 13:56)

Any tips on how to do this ???

Thanks
 
Try

actdate = new DateTime(dateAsString.SubString(4,2),
dateAsString.SubString(2,2), dateAsString.SubString(0,2),
timeAsString.SubString(0,2), timeAsString.SubString(2,2), 0)

Good luck,
Johnny J.
 
Try

actdate = new DateTime(dateAsString.SubString(4,2),
dateAsString.SubString(2,2), dateAsString.SubString(0,2),
timeAsString.SubString(0,2), timeAsString.SubString(2,2), 0)

Do you have Option Strict On? The code you posted will not work
because the DateTime constructor does not take strings as any of its
arguments. Instead, it would be better use the TryParseExact method:

Imports System.Globalization

Dim stringDate As String = "161009"
Dim stringTime As String = "1356"

Dim parsedDate As DateTime
Dim parsedTime As DateTime

If DateTime.TryParseExact(stringDate, "ddMMyy",
CultureInfo.InvariantCulture, DateTimeStyles.None, parsedDate) Then
MsgBox("Date was parsed successfully")
Else
MsgBox("Date was not parsed successfully")
End If

If DateTime.TryParseExact(stringTime, "HHmm",
CultureInfo.InvariantCulture, DateTimeStyles.None, parsedTime) Then
MsgBox("Time was parsed successfully")
Else
MsgBox("Time was not parsed successfully")
End If


Chris
 
Of course not - how silly I am. I was writing off the top of my head and
forgot to cast the substrings as integer.

But if you do that, then it will work...

Cheers,
Johnny J.
 
Hi Chris,

Although English is of course an invariant language is in my idea that extra
for Culture Invariant made for persons who never came on the continent.

Noting is enough in that, even on the continent is a day a day, a month a
month and a year a year.

Only somebody not common with languages would invent something like this and
only someone only speaking English would use it like you. Are you an
Englishman (I know it).

:-)

Cor
 
That usage is correct. CultureInvariant is used to advise the compiler that
no culture should be considered for the parsing, and that the format string
is to be used exactly as provided. This means that the runtime will not
apply any culture-dependant processing to the parsing, which is what is
required in this case.

Your zenophobic rant simply reveals that you don't know how to use correctly
the options available in the TryParse method.
 
This can only be written by somebody, who lives in a very enclosed culture,
where are you from James?

The culture invariant is simply because that English has two ways of
representing a date MM-dd-yyyy and dd-MM-yyyy.

The most natively English speaking persons use the first variant.

However, if you tell that which variant culture variant is exactly used,
there is no need to tell which variant is used, some strange thinking of
many persons who know only the English language and think that it's the only
language on earth or that it is the major language.
 
That is not correct. You have misunderstood the use of the
CultureInfo.InvariantCulture property and your personal prejudices are
preventing you from recognising that.

The whole point of CultureInfo.InvariantCulture is to allow the programmer
to define and enforce a format for dates as strings (such as the literal
ddmmyy as in this case) that will function properly regardless of any
changes made to a user's culture settings. It enforces a consistency that
ensures that dates stored as strings will always convert correctly
regardless of whether the user's culture is specified as a specific culture
or as the user's variation of a specific culture.

If you are seeing it a some form of conspiracy to make the default format of
mm/dd/yyyy universal then that is because you are using it incorrectly as
part of formatting dates for display. The only thing that
CultureInfo.InvariantCulture has to do with displaying dates is in assisting
the programmer to ensure that dates stored as strings are always displayed
exactly as the user's culture info settings require. And that is how it has
been used in the above example.
 
Back
Top