Solved

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

Posted on 2009-05-04
6
502 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:andrewmilner
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24298357
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

0
 
LVL 8

Assisted Solution

by:ppittle
ppittle earned 200 total points
ID: 24298523
AndrewMilner,

This can be a nasty problem.  There's a .NET class that will take care of you called TimeZoneInfo, but it's only available in .NET 3.5.  If you are using an older version of the framework, you'll have to do some work.  Take a look at this thread on MSDN:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/f89248c2-e427-4333-9b7d-97f0fbd1ca65/
0
 

Author Comment

by:andrewmilner
ID: 24298794
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?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:andrewmilner
ID: 24298801
sorry, no its not a string its a DateTime thats how its being passed but in en-gb format as set in web config.
0
 
LVL 15

Accepted Solution

by:
oobayly earned 300 total points
ID: 24298816
I'd absolutely use Parameters when creating any SQL commands. This will get around any globalisation issues. It's also a good way against protecting against injection attacks.
0
 

Author Comment

by:andrewmilner
ID: 24299129
Cool thanks.  I'll try the parameters.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now