changing the date format in vb.net

A

andreas

When I copy a vb.net project using date formats from one PC with a windows
date format f.e. dd/mm/yyyy to another PC having a format yy/mm/dd then I
get errors.
How can I change for a while in the project the date format in vb.code (
not in Windows) and how can I find out which date format the PC Windows is
using.
Thanks for any response
 
G

Guest

instead of using or changing the date, why not make your own?

Dim str as String = Now.Day & "/" & Now.Month & "/" & Now.Year

which gives:

dd/mm/yy
 
A

andreas

That don't work
When i put in dd/mm/yy in my vb/.net program (on the other PC) is thinking
that is is yy/mm/dd so there is a error
There is some code for it with the word globalisation but I don't know the
code
Thanks any way
 
C

Cor Ligthert [MVP]

Andreas,

Strange, I can assure you that Ken and I exhange programs on regular base.

He is in the US and I in the EU. We never had any problem with datetime.

That would be crazy because in datetime there is not in a special culture.
It is a "long" which holds a counter of 100 nanoseconds ticks that started
(in Net) at the Christian calendar date 01/01/01 00:00:00. In other systems
the starting time and way it is done can be different.

But never try to convert the datetime to string formats with another reasons
than to show those.

I hope this helps,

Cor
 
A

andreas

When the date format is changed in windows (configuration panel) i got a
error on a different PC and on my own PC the dates with month = 25,13,..
and so on where not listened.
It depens likely on the settings mm/dd/yyyy or yyy/mm/dd.
And that is my question for a solution in vb.net to detect and to change
these PC settings
 
H

Herfried K. Wagner [MVP]

andreas said:
When I copy a vb.net project using date formats from one PC with a windows
date format f.e. dd/mm/yyyy to another PC having a format yy/mm/dd then I
get errors.
How can I change for a while in the project the date format in vb.code (
not in Windows) and how can I find out which date format the PC Windows is
using.

'System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat'.

Using a culture-specific date/time format to persist dates is never a good
idea if data is exchanged between machines which are potentially running
with different date/time formats. Instead, use
'CultureInfo.InvariantCulture.DateTimeFormat' to format the dates when
persisting them and use the invariant culture to read the date values later
on.
 
C

Cor Ligthert [MVP]

Herfried,

The invarianculture in case of datetime is associated with the English
Language where you can have dependend of the culture june 15 and 15 june.

This makes it possible to do a dataparse.time(date in written words) for
both culture systems in the English language.

Dates written in German needs the parameter in that case that it is in the
German culture, however if that is the local setting than Nothing is more
than enough (while invariantculture will than have the same effect as
nothing because the culture is already German). The last is probably your
situation.

The system does not automaticly recognise that by instance "1/12/2006" is
one time 12 december and anoter time januari 12 how much time you write
that it is an invariant culture.

Cor
 
C

Cor Ligthert [MVP]

Andreas,

Did you set a string to a datetime somewhere?

By instance a command like
dim a as date = CDate(25/06/2006)"
or the same with
DateTime.parse
or with
DateTime.Convert
or with
CType("date",datetime)

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
The invarianculture in case of datetime is associated with the English
Language where you can have dependend of the culture june 15 and 15 june.

The invariant culture is invariant by definition and it's unique, which
means that it doesn't suffer from the problems other cultures suffer from.
If June, 15th is persisted using the invariant culture and later read using
the invariant culture, it will always result in June, 15th.
This makes it possible to do a dataparse.time(date in written words) for
both culture systems in the English language.

Why would you want to mix up different cultures?
'CultureInfo.InvariantCulture' has (by definition) as much to do with 'de'
as with 'en'. It's simply a separate culture.
Dates written in German needs the parameter in that case that it is in the
German culture, however if that is the local setting than Nothing is more
than enough (while invariantculture will than have the same effect as
nothing because the culture is already German).

?!? 'InvariantCulture' is a completely independent culture. It has the
same semantics on all systems and thus will never be the same as another
culture.
The system does not automaticly recognise that by instance "1/12/2006" is
one time 12 december and anoter time januari 12 how much time you write
that it is an invariant culture.

If you have a 'Date' variable representing '#1/12/2006#' (written as VB date
literal) and persist it using the invariant culture and later use the
invariant culture to read the persisted value the resulting 'Date' object
will have the value '#1/12/2006#'. One doesn't have to care about and know
the format used to persist the date using the invariant culture. The
invariant culture's main purpose is to make exchange of data between
different systems running with different culture settings easier by removing
any ambiguities potentially caused by different culture settings.
 
C

Cor Ligthert [MVP]

Herfried,

What does this text in the description page of DateTime mean for you.

Each DateTime member implicitly uses the Gregorian calendar to perform its
operation, with the exception of constructors that specify a calendar and
methods that implicitly specify a calendar with a parameter derived from
IFormatProvider, such as System.Globalization.DateTimeFormatInfo. Use the
System.Globalization.Calendar class to perform date and time operations with
a different calendar.
*methods* that implicitly specify a calendar etc.

The description from the datetime is:
The DateTime value type represents dates and times with values ranging from
12:00:00 midnight, January 1, 0001 Anno Domini (Common Era) to 11:59:59
P.M., December 31, 9999 A.D. (C.E.)

Time values are measured in 100-nanosecond units called ticks, and a
particular date is the number of ticks since 12:00 midnight, January 1, 1
A.D. (C.E.) in the GregorianCalendar calendar. For example, a ticks value of
31241376000000000L represents the date, Friday, January 01, 0100 12:00:00
midnight. A DateTime value is always expressed in the context of an explicit
or default calendar.

As I read it as you write, than you give me the idea that for you a datetime
is stored in the way as in the literals. The dateobject does not have the
value '#1/12/2006#' it has just a counter which has a starting point at
01/01/00 00:00:00 in our notation in ISO.

I could not find any member for a stored DateTimeFormatInfo inside the
DateTime structure

Cor
 
A

andreas

Yes
I have to read a textdate from a textfile into a textbox and I would'n
change my textfile.
I put it again, I like to change the windows date format with a command in
my vb.net program so that I can run my program on any PC.
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
I could not find any member for a stored DateTimeFormatInfo inside the
DateTime structure

Nobody's claiming that. 'DateTime' per-se doesn't have to do anything with
a specific culture. Cultures come into the play when formatting the date to
persist it, for example.
 
C

Cor Ligthert [MVP]

Andreas,

Than you have to know the format how that date is written and use the
overloaded command.

datetime.parseexact

http://msdn.microsoft.com/library/d.../frlrfsystemdatetimeclassparseexacttopic1.asp

If you are sure that the dates are only in numbers than the last operand can
be nothing.

Be aware that this is only in the case of by instand textfiles to datetime.

In another culture will the user in the UI type the date in the style he is
used to, therefore from a textbox a CDate works perfect.

I hope this hels,

Cor
 
A

andreas

Herfried and Cor,
Thanks, it is very interesting but I want ( if possible) a direct answer.

When can change in Windows - Control panel the format of the date and it is
important for vb.net when reading a date in plain tekst in a textfile
When the windows format is f.e. yyyy/mm/dd and the textfile gives in plain
tekst 31/12/2005 then I get a error.
When I change the windows format in dd/mm/yyyy than the date is reading.
Is it possible and how can I :
Dedect in VB.net what the setting is in O.S.
Change this setting in what I want in vb.net at the beginning of the
program.
 
C

Cor Ligthert [MVP]

Andreas,

Newsgroups are meant as a communitiy, where problems can be discussed in any
way by the contributers who want to learn from each other.

If you want only a direct answer to your questions, than you can better go
to a paid Microsoft Support Center or hire in another way an advisor.

Just my thought,

Cor
 
H

HKSHK

Hi, Andreas:

To retrieve the date format for the current user use this:
Dim tdfi As New System.Globalization.DateTimeFormatInfo
Dim dtFormat As String = tdfi.CurrentInfo.ShortDatePattern

dtFormat then contains the string you're looking for
(e.g. "dd.MM.yyyy"). This is the format of the user settings for the
account the program is running under. So this might only help you to
write the format into a file, but not to recognize the format in the file.

But I wonder, if you receive the file with those data, don't you know
the date format? If not, you might want to ask the person who sends you
the file to include an .INI file with the date format or to write the
date format into the first line of the file. If you know the date format
you can process it like this:

Dim FlatFileDate As String, DisplayDate As String
Dim tMonth As Integer, tDay As Integer, tYear As Integer

FlatFileDate = "06252006" '=MMDDYYYY, that's what you read from your
' file

'Lets say you want to convert to YYYYMMDD

tMonth = Val(Strings.Left(FlatFileDate, 2))
tDay = Val(Strings.Mid(FlatFileDate, 3, 2))
tYear = Val(Strings.Right(FlatFileDate, 4))

Dim dt As New Date(tYear, tMonth, tDay) 'Creates a date variable with
'the date you just processed.

DisplayDate = Format(dt, "Short Date") 'Formats the date to local short
'date

MsgBox(DisplayDate)

Best Regards,

HKSHK
 
H

Herfried K. Wagner [MVP]

andreas said:
When can change in Windows - Control panel the format of the date and it
is
important for vb.net when reading a date in plain tekst in a textfile
When the windows format is f.e. yyyy/mm/dd and the textfile gives in plain
tekst 31/12/2005 then I get a error.
When I change the windows format in dd/mm/yyyy than the date is reading.
Is it possible and how can I :
Dedect in VB.net what the setting is in O.S.

You don't need to worry about the OS settings. Just set the application's
'CurrentCulture' to the culture you want to use to persist the data and then
reset it to its previous value.
 
A

andreas

Cor,
There is a misunderstanding.
I don't need a personal message but in the newsgroup codelines who are a
solution for my questions.
I see now a answer from herfried which I will try (if I can)
 
A

andreas

Herfried,
Thanks and I will try your solution.

Herfried K. Wagner said:
You don't need to worry about the OS settings. Just set the application's
'CurrentCulture' to the culture you want to use to persist the data and then
reset it to its previous value.
 
A

andreas

Thanks very much HKSHK ! (and all the others to)
This is the best answer I recieved to my questions!
Thanks a lot again
 

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