DateTime Problem

T

thomson

Hi All,
i have a date time object which i have declared as

DateTime dtFromDate,


and i do have a textbox which has a string 31/03/2005, which is of
dd/mm/yyyy,

while iam assigning this value

like this

dtFromDate = DateTime.Parse(txtLastDate.Value);

it gives me an error input string not in a correct format,

but it works if i have a string of mm/dd/yyyy,


how should i go for this


Thanks in Advance


Thomson
 
E

Eran Kampf

Hi,
When you use DateTime.Parse(string) it uses the computer's default locale
which in your case is mm\dd\yyyy.
To avoid this sort of problem you should send DateTime.Parse an
IFormatProvider of the current culture you're expecting.
You can also use this (for example):
DateTime.ParseExact(txtLastDate.Value,"dd/MM/yyyy",System.Globalization.Cult
ureInfo.InvariantCulture);

Eran Kampf
http://www.ekampf.com/
http://www.ekampf.com/blog/
 
C

Cor Ligthert

Thomson,

When you are developing on an computer with a English Windows version, while
your own culture is another, is in my opinion the best thing you can do is
setting your datetime settings from your own developping computer in the
(configiration) country settings to the right culture.

The change with fixed date paterns and therefore errors with your client
will be otherwise easy possible.

Just my opinion

Cor
 
G

Guest

HI thomson,

This is a common problem faced by many programmers. You are getting this
error because your current local setting for date is mm/dd/yy. When you pass
31/03/2005, it considers 31 as the month which is wrong. The best and the
easiest method would be to always pass the format in yyyy/mm/dd. I have
learned this through my experience and i bet you will be always safe by
passing to any datetime object in this format.

happy programming
pradeep_TP
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Use DateTime.ParseExact , it let you especify the format expected.

In any way you better handle the case of a incorrect format date and do
something about it, probably you should use Validating or LostFocus events
to make the check.


cheers,
 

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

Similar Threads


Top