473,396 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Convert en-GB to en-US

How do I convert a datetime from en-GB to en-US format

here is my code - (not working for the clients outside of US)
/*******************CODE*****************/
System.Globalization.CultureInfo myCI = new
System.Globalization.CultureInfo("en-US", true);
xwriter.WriteElementString(""xxx",
System.DateTime.Parse(Convert.ToDateTime("16/2/2006").ToShortDateString(),
myCI).ToString());
/*******************************************/

Error = System.FormatException: String was not recognized as a valid
DateTime.

Jan 3 '06 #1
5 24048
Hi rsanan,
you have your logic the wrong way round. If you have an en-UK format
string, then to Convert.ToDateTime you need to pass in the local you are
expecting to parse, which will be english-uk, you can then print out the date
once it is parsed in en-us. i.e.

System.Globalization.CultureInfo enUk = new
System.Globalization.CultureInfo("en-GB");

DateTime dt = Convert.ToDateTime("16/2/05", enUk);

Console.WriteLine(dt.ToString(new
System.Globalization.CultureInfo("en-US")));
Hope that helps
Mark Dawson
http://www.markdawson.org

"rs****@gmail.com" wrote:
How do I convert a datetime from en-GB to en-US format

here is my code - (not working for the clients outside of US)
/*******************CODE*****************/
System.Globalization.CultureInfo myCI = new
System.Globalization.CultureInfo("en-US", true);
xwriter.WriteElementString(""xxx",
System.DateTime.Parse(Convert.ToDateTime("16/2/2006").ToShortDateString(),
myCI).ToString());
/*******************************************/

Error = System.FormatException: String was not recognized as a valid
DateTime.

Jan 3 '06 #2
rs****@gmail.com <rs****@gmail.com> wrote:
How do I convert a datetime from en-GB to en-US format

here is my code - (not working for the clients outside of US)
/*******************CODE*****************/
System.Globalization.CultureInfo myCI = new
System.Globalization.CultureInfo("en-US", true);
xwriter.WriteElementString(""xxx",
System.DateTime.Parse(Convert.ToDateTime("16/2/2006").ToShortDateString(),
myCI).ToString());
/*******************************************/

Error = System.FormatException: String was not recognized as a valid
DateTime.


Specify the culture-info for both the parsing and the formatting.
There's no need to parse and format twice (which is what you're doing).
One reason it's not clear what's going on is that you've got four
method calls in the same statement - it's worth breaking them up for
the sake of readability.

Here's a working example:

using System;
using System.Globalization;

public class Test
{
static void Main()
{
string original = "16/2/2006";
CultureInfo gb = new CultureInfo ("en-GB");
CultureInfo us = new CultureInfo ("en-US");

DateTime parsed = DateTime.Parse (original, gb);

string formatted = parsed.ToString("d", us);

Console.WriteLine (formatted);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 3 '06 #3
Mark and Jon,
Thanks for putting me in the right direction - It works fine!
I went ahead and modified Jon's code to generalize the original date to
any Culture (as my clients may be in any country).
Here is the snippet for any one who might need

/************CODE***********/
using System;
using System.Globalization;
public class Test
{
static void Main()
{
string original = "16/2/2006"; //could be in any format
CultureInfo other = new CultureInfo
(CultureInfo.CurrentCulture.Name);
CultureInfo us = new CultureInfo ("en-US");
DateTime parsed = DateTime.Parse (original, other);
string formatted = parsed.ToString("d", us);
Console.WriteLine (formatted);
}
}
/*************************************/

Jan 4 '06 #4
rs****@gmail.com wrote:
Mark and Jon,
Thanks for putting me in the right direction - It works fine!
I went ahead and modified Jon's code to generalize the original date to
any Culture (as my clients may be in any country).
Here is the snippet for any one who might need

<snip>
CultureInfo other = new CultureInfo
(CultureInfo.CurrentCulture.Name);


Why create a new CultureInfo rather than just using
CultureInfo.CurrentCulture?

Jon

Jan 4 '06 #5
Better still, Thanks Jon!

/************CODE***********/
using System;
using System.Globalization;
public class Test
{
static void Main()
{
string original = "16/2/2006"; //could be in any format
CultureInfo us = new CultureInfo ("en-US");
DateTime parsed = DateTime.Parse(original,
CultureInfo.CurrentCulture);
string formatted = parsed.ToString("d", us);
Console.WriteLine (formatted);
}
}
/*************************************/
rs****@gmail.com
Jan 4, 10:17 am show options

Newsgroups: microsoft.public.dotnet.languages.csharp
From: "rsa...@gmail.com" <rsa...@gmail.com> - Find messages by this
author
Date: 4 Jan 2006 07:17:41 -0800
Local: Wed, Jan 4 2006 10:17 am
Subject: Re: Convert en-GB to en-US
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse

Mark and Jon,
Thanks for putting me in the right direction - It works fine!
I went ahead and modified Jon's code to generalize the original date to

any Culture (as my clients may be in any country).
Here is the snippet for any one who might need

Jan 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below....
1
by: Logan X via .NET 247 | last post by:
It's official....Convert blows. I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance...
4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
5
by: Cally | last post by:
Hello, I would like to convert a field from ntext field found in one database table to float field found in another database table. The reason why I want to do this is a long one. I have...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
7
by: patang | last post by:
I want to convert amount to words. Is there any funciton available? Example: $230.30 Two Hundred Thirty Dollars and 30/100
6
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function...
2
by: kirke | last post by:
Hi, I have a datetime column named dtDateTime. its format is "Oct 27 2006 12:00:00 " I want to group by only date part of it and count my code is $sql1="SELECT ...
1
by: johnlim20088 | last post by:
Hi, Currently I have 6 web projects located in Visual Source Safe 6.0, as usual, everytime I will open solution file located in my local computer, connected to source safe, then check out/check in...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.