By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,603 Members | 1,088 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,603 IT Pros & Developers. It's quick & easy.

Date Time Format Problem

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies

P: n/a
"okaminer" <ok*******@yahoo.com> schrieb
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

Nov 21 '05 #2

P: n/a
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

Nov 21 '05 #3

P: n/a
"okaminer" <ok*******@yahoo.com> schrieb
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

Nov 21 '05 #4

P: n/a
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
Nov 21 '05 #5

P: n/a
Hello,

You can use

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

~

to get DateTime from custom format.

HTH

Roman

"okaminer" <ok*******@yahoo.com> сообщил/сообщила в новостях следующее:
news:11**********************@g43g2000cwa.googlegr oups.com...
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

Nov 21 '05 #6

P: n/a
"Dragon" <no@spam.please> schrieb
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

Nov 21 '05 #7

P: n/a
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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:u8**************@TK2MSFTNGP14.phx.gbl...
| "Dragon" <no@spam.please> schrieb
| > 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
|
Nov 21 '05 #8

P: n/a
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb
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

Nov 21 '05 #9

P: n/a
> 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.
Nov 21 '05 #10

P: n/a
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/de...ratorTopic.asp

And Custom DateTime Format Strings:

http://msdn.microsoft.com/library/de...matstrings.asp

For details.

Hope this helps
Jay
"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb
| > 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
|
Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.