Date Time Format Problem

O

okaminer

Hi
I have a program which need to get the date in the format of dd/MM/yyyy
(example 24/7/2005)

but when I use DateTime.Now().ToShortDatetime() the date come back as
MM/dd/yyyy (example 7/24/2005)

This cause much problems

I would like to know how to change the .NET regional setting so the
date will come back as dd/MM/yyyy

either by changing the computer settings or by code
 
A

Armin Zingler

okaminer said:
Hi
I have a program which need to get the date in the format of
dd/MM/yyyy (example 24/7/2005)

but when I use DateTime.Now().ToShortDatetime() the date come back
as MM/dd/yyyy (example 7/24/2005)

This cause much problems

I would like to know how to change the .NET regional setting so the
date will come back as dd/MM/yyyy

either by changing the computer settings or by code


You should leave the computer settings because the user will probably not be
amused if you change them.

Instead of ToShortDatetime, use ToString(format). See <f1> for available
formats, eg. "dd\/M\/yyyy", or "d\/M\/yyyy"

Armin
 
O

okaminer

Thanks for replying

I don't mind changing the computer settings since this will not run on
client compter but it is a web application which will run on the
server/
Also I am using the function ToString("dd/MM/yyyy"), but when I try to
put this value back to a DateTime parameter, the computer gets an error
because he thinks that in 24/7/2005 the 24 is Month (and clearlly this
is an error because there is no month 24)

So I really need to know how to change to computer setting from code
or from control panel

thanks again
 
A

Armin Zingler

okaminer said:
Thanks for replying

I don't mind changing the computer settings since this will not run
on client compter but it is a web application which will run on the
server/
Also I am using the function ToString("dd/MM/yyyy"), but when I try
to put this value back to a DateTime parameter, the computer gets an
error because he thinks that in 24/7/2005 the 24 is Month (and
clearlly this is an error because there is no month 24)

Let me clarify: Your problem is not converting it to a string but from a
string to date/time, right?

How did you try to convert it to DateTime? It's pretty easy to extract the
values from the string if the string is not supposed to be formatted as
specified in the system's settings. Example:

Private Shared Function GetDateTimeFromDMYString( _
ByVal s As String, _
Optional ByVal Seperator As Char = "/"c) _
As Date

Dim parts As String()
parts = s.Split(Seperator)
Return New DateTime( _
Integer.Parse(parts(0)), _
Integer.Parse(parts(1)), _
Integer.Parse(parts(2)) _
)
End Function

So I really need to know how to change to computer setting from
code or from control panel

Sorry, I don't know. Probably using API functions.


Armin
 
C

Cor Ligthert [MVP]

Okamir,

In addition to Armin, I never will set using a program the country/date
settings of a computer from a user.

However doing it by the control panel is in the advance country settings (NT
based computers). (Never do this without that the user knows this. Otherwise
it can cost you a lot of money).

I hope this helps,

Cor
 
D

Dragon

Hello,

You can use

~
DateTime.ParseExact("24/7/2005", "dd/M/yyyy",
Globalization.DateTimeFormatInfo.InvariantInfo)

~



to get DateTime from custom format.

HTH

Roman
 
A

Armin Zingler

Dragon said:
Hello,

You can use

~
DateTime.ParseExact("24/7/2005", "dd/M/yyyy",
Globalization.DateTimeFormatInfo.InvariantInfo)

~



to get DateTime from custom format.


First I wanted to suggest ParseExact too, but it doesn't work if it's
"24/12/2005", thus the self-written function.


Armin
 
J

Jay B. Harlow [MVP - Outlook]

Armin,
| First I wanted to suggest ParseExact too, but it doesn't work if it's
| "24/12/2005", thus the self-written function.

Really! which version of the framework?

Dim ChristmasEve As DateTime = DateTime.ParseExact("24/12/2005",
"dd/M/yyyy", Globalization.DateTimeFormatInfo.InvariantInfo)
Debug.WriteLine(ChristmasEve.ToLongDateString())


Returns: Saturday, December 24, 2005 on .NET 1.1 (in the US, in other
countries it will Return Dec 24th 2005 in possibly other formats)!

Hope this helps
Jay





| > Hello,
| >
| > You can use
| >
| > ~
| > DateTime.ParseExact("24/7/2005", "dd/M/yyyy",
| > Globalization.DateTimeFormatInfo.InvariantInfo)
| >
| > ~
| >
| >
| >
| > to get DateTime from custom format.
|
|
| First I wanted to suggest ParseExact too, but it doesn't work if it's
| "24/12/2005", thus the self-written function.
|
|
| Armin
|
 
A

Armin Zingler

Jay B. Harlow said:
Armin,
| First I wanted to suggest ParseExact too, but it doesn't work if
it's | "24/12/2005", thus the self-written function.

Really! which version of the framework?

Dim ChristmasEve As DateTime =
DateTime.ParseExact("24/12/2005", "dd/M/yyyy",
Globalization.DateTimeFormatInfo.InvariantInfo)
Debug.WriteLine(ChristmasEve.ToLongDateString())


Returns: Saturday, December 24, 2005 on .NET 1.1 (in the US, in
other countries it will Return Dec 24th 2005 in possibly other
formats)!


My fault. I passed 'Nothing' as the 3rd argument. Passing InvariantInfo
works.

I don't understand why this makes the difference. Maybe the formats are
different, i.e. the order of month, day, year or the seperator is different,
but why does one allow a single digit only while the other one accepts one
or two digits? I thought (Parse)exact means (Parse)exact.


But wait... this also works:

d = DateTime.ParseExact("24/10/2005", "d\/M\/yyyy", Nothing)
d = DateTime.ParseExact("4/10/2005", "d\/M\/yyyy", Nothing)
d = DateTime.ParseExact("24/1/2005", "d\/M\/yyyy", Nothing)
d = DateTime.ParseExact("4/1/2005", "d\/M\/yyyy", Nothing)

Same format string independent from number of digits per day or month. :)


Armin
 
D

Dragon

I don't understand why this makes the difference. Maybe the formats are
different, i.e. the order of month, day, year or the seperator is
different,

It seems that separator is the problem.
These works on my machine (Russian locale => date format is "dd.MM.yyyy"):

~
DateTime.ParseExact("24/01/1995", "dd/MM/yyyy",
Globalization.DateTimeFormatInfo.InvariantInfo)

DateTime.ParseExact("24.01.1995", "dd/MM/yyyy", Nothing)

~

And this doesn't work:

~
DateTime.ParseExact("24/01/1995", "dd/MM/yyyy", Nothing)

~
but why does one allow a single digit only while the other one accepts one
or two digits? I thought (Parse)exact means (Parse)exact.

AFAIK, M means one digit if it's possible, or two otherwise, and MM — always
two digits.
In ToString method you would probably use MM for better looking, and in
ParseExact M to prevent parsing errors.
 
J

Jay B. Harlow [MVP - Outlook]

Armin,
As Dragon suggests:
| My fault. I passed 'Nothing' as the 3rd argument. Passing InvariantInfo
| works.
If you pass Nothing for the 3rd argument it means to use
CultureInfo.CurrentCulture.

Which may have its DateTimeFormatInfo set to expect something other the /
for the date separator (in the case of Europe & Russia verses the US).

I normally use CultureInfo.InvariantInfo (which has an invariant
DateTimeFormatInfo object attached) instead of
DateTimeFormatInfo.InvariantInfo. In either case
DateTimeFormatInfo.InvariantInfo says to expect / for the date separator.

The "/" in "dd/M/yyyy" says to use the date separator from the
DateTimeFormatInfo object passed, while the "\/" in "d\/M\/yyyy" says to use
a "/" literally as the date separator.

See DateTimeFormatInfo.DateSeparator:

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

And Custom DateTime Format Strings:

http://msdn.microsoft.com/library/d...ide/html/cpconcustomdatetimeformatstrings.asp

For details.

Hope this helps
Jay


| > Armin,
| > | First I wanted to suggest ParseExact too, but it doesn't work if
| > it's | "24/12/2005", thus the self-written function.
| >
| > Really! which version of the framework?
| >
| > Dim ChristmasEve As DateTime =
| > DateTime.ParseExact("24/12/2005", "dd/M/yyyy",
| > Globalization.DateTimeFormatInfo.InvariantInfo)
| > Debug.WriteLine(ChristmasEve.ToLongDateString())
| >
| >
| > Returns: Saturday, December 24, 2005 on .NET 1.1 (in the US, in
| > other countries it will Return Dec 24th 2005 in possibly other
| > formats)!
|
|
| My fault. I passed 'Nothing' as the 3rd argument. Passing InvariantInfo
| works.
|
| I don't understand why this makes the difference. Maybe the formats are
| different, i.e. the order of month, day, year or the seperator is
different,
| but why does one allow a single digit only while the other one accepts one
| or two digits? I thought (Parse)exact means (Parse)exact.
|
|
| But wait... this also works:
|
| d = DateTime.ParseExact("24/10/2005", "d\/M\/yyyy", Nothing)
| d = DateTime.ParseExact("4/10/2005", "d\/M\/yyyy", Nothing)
| d = DateTime.ParseExact("24/1/2005", "d\/M\/yyyy", Nothing)
| d = DateTime.ParseExact("4/1/2005", "d\/M\/yyyy", Nothing)
|
| Same format string independent from number of digits per day or month. :)
|
|
| Armin
|
 

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