Convert en-GB to en-US

R

rsanan

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.
 
J

Jon Skeet [C# MVP]

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);
}
}
 
G

Guest

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
 
R

rsanan

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);
}
}
/*************************************/
 
J

Jon Skeet [C# MVP]

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

CultureInfo other = new CultureInfo
(CultureInfo.CurrentCulture.Name);

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

Jon
 
R

rsanan

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);
}
}


/*************************************/
(e-mail address removed)
Jan 4, 10:17 am show options

Newsgroups: microsoft.public.dotnet.languages.csharp
From: "(e-mail address removed)" <[email protected]> - 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top