472,096 Members | 1,193 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 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 23811
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Lauren Quantrell | last post: by
1 post views Thread by Logan X via .NET 247 | last post: by
3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by
7 posts views Thread by patang | last post: by
6 posts views Thread by patang | last post: by
2 posts views Thread by kirke | last post: by
reply views Thread by leo001 | last post: by

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.