Solved

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

Posted on 2009-05-04
6
504 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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