?
Solved

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

Posted on 2009-05-04
6
Medium Priority
?
515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 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