tags:

views:

88

answers:

5

hi

i receive this date: 9/20/2010 3:32:32 PM

i need to convert to datetime.

i try:

DateTime DateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM", "dd/M/yyyy", CultureInfo.InvariantCulture);

but i get error: String was not recognized as a valid DateTime.

in my computer the region is: Hebrew (Israel) dd/MM/yyyy for short date and hh:mm for short time

how to fix it ?

thank's in advance

A: 

It looks like your original date string is in a US format (i.e. m/dd/yyyy). Try replacing your third parameter with new CultureInfo("en-US")

Traveling Tech Guy
That's not going to help when he's explicitly expecting "dd/M/yyyy" in the format string.
Jon Skeet
+11  A: 

If you're receiving "9/20/2010 3:32:32 PM" as a string, then trying to parse it as if it were in the "dd/MM/yyyy" format is clearly wrong - that's try to use a month of 20. You're also only parsing part of the string - you need to either trim your string or provide the complete format.

Try this:

DateTime dateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM",
                                        "M/dd/yyyy h:mm:ss tt", 
                                        CultureInfo.InvariantCulture);

Note that using this sort of strict parsing will only work if you can guarantee that that will always be the format. Where are you getting this data from?

Jon Skeet
darn you are too quick for me! Was about to post the exact same comment :-)edit - just tested it... didn't work
Xander
thank's for the help, but still same Error
Gold
it needs two 't' at the end of the format.. i.e. "M/dd/yyyy h:mm:ss tt"
Xander
@Xander: Fixed, thanks.
Jon Skeet
@Gold: See edit - using "tt" instead of "t".
Jon Skeet
@Jon: No problem... :-)
Xander
+4  A: 

Hello,

How could it work man.You are converting into "dd/MM/yyyy " & putting month as 20.In your question dd/M/yyyy is wrong.It will like dd/MM/yyyy.

By default format is MM/DD/yyyy.

simple way to do .......

DateTime DateFrom = DateTime.Parse("9/20/2010 3:32:32 PM");

if you want to provide a specific Format so use like that

DateTime DateFrom = DateTime.ParseExact("20/09/2010 3:32:32 PM", "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);

I hope it works.

PrateekSaluja
@PrateekSaluja: That won't work, as it's trying to parse a date and time as just a date.
Jon Skeet
Default is only `MM/dd/yyyy` if your culture settings specify that.
ck
PrateekSaluja
@Jon Skeet: Now i hope it works.
PrateekSaluja
@Prateek: Yes, I'd expect it to work now. Although it doesn't solve the actual problem, because you're not parsing the actual data he's received...
Jon Skeet
thank's it works !!!how to show the time like this: hh:mm(without the second)
Gold
@Gold: that's another question, I suggest raising another question (after searching for one that does not already exist) :-)
Xander
Hi try this to get time string time=DateTime.Now.ToShortTimeString();
PrateekSaluja
A: 
DateTime dateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM", "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);

Works for me

Xander
p.s. credit to Jon Skeet for getting there first (I just made a slight change)
Xander
A: 

I wouldn't use ParseExact() when I know that time string is formatted by invariant culture.

DateTime dateFrom = DateTime.Parse(dateString, CultureInfo.InvariantCulture);

is both more compact and more clear.

Paweł Dyda