Solved

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

Posted on 2009-05-04
6
500 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
How to open/READ file from UNC path Shared Folder... 2 39
Windows Service with UDP 2 28
Birthdays 3 14
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now