Solved

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

Posted on 2009-05-04
6
508 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
Industry Leaders: 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 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

697 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