Link to home
Start Free TrialLog in
Avatar of andrewmilner
andrewmilner

asked on

c# Converting DateTime from en-gb to en-us

I have an en-gb web app that is posting data to a WebService that then needs to convert the posted en-gb datetime to en-us before putting it in the database.

How would I do that with C# code?
Avatar of oobayly
oobayly
Flag of United Kingdom of Great Britain and Northern Ireland image

If it's a web service, shouldn't the date be sent as a DateTime parameter? Also it shouldn't matter whether it's in GB or US format as it's stored as a DateTime object too.

If you're using strings, you could use DateTime.ParseExact
Dim s As String = DateTime.Now("dd/MM/yyyy HH:mm:ss")
Dim d As DateTime = DateTime.ParseExact(s, "dd/MM/yyyy HH:mm:ss", System.Globalization.DateTimeFormatInfo.CurrentInfo)

Open in new window

SOLUTION
Avatar of ppittle
ppittle
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andrewmilner
andrewmilner

ASKER

The DateTime is being passed as a string but I'm building up a command to insert the SQL
like set TheDateTime = " + DateTime " 
And because my local is set to en-gb it's therefore dumping the datetime into the sql insert statement as en-gb which the sql server rejects.

If I used parameters on the Insert statement would this go away?
sorry, no its not a string its a DateTime thats how its being passed but in en-gb format as set in web config.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cool thanks.  I'll try the parameters.