473,574 Members | 14,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Globaliz ation.CultureIn fo myCI = new
System.Globaliz ation.CultureIn fo("en-US", true);
xwriter.WriteEl ementString(""x xx",
System.DateTime .Parse(Convert. ToDateTime("16/2/2006").ToShortD ateString(),
myCI).ToString( ));
/*************** *************** *************/

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

Jan 3 '06 #1
5 24077
Hi rsanan,
you have your logic the wrong way round. If you have an en-UK format
string, then to Convert.ToDateT ime 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.Globaliz ation.CultureIn fo enUk = new
System.Globaliz ation.CultureIn fo("en-GB");

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

Console.WriteLi ne(dt.ToString( new
System.Globaliz ation.CultureIn fo("en-US")));
Hope that helps
Mark Dawson
http://www.markdawson.org

"rs****@gmail.c om" 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.Globaliz ation.CultureIn fo myCI = new
System.Globaliz ation.CultureIn fo("en-US", true);
xwriter.WriteEl ementString(""x xx",
System.DateTime .Parse(Convert. ToDateTime("16/2/2006").ToShortD ateString(),
myCI).ToString( ));
/*************** *************** *************/

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

Jan 3 '06 #2
rs****@gmail.co m <rs****@gmail.c om> 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.Globaliz ation.CultureIn fo myCI = new
System.Globaliz ation.CultureIn fo("en-US", true);
xwriter.WriteEl ementString(""x xx",
System.DateTime .Parse(Convert. ToDateTime("16/2/2006").ToShortD ateString(),
myCI).ToString( ));
/*************** *************** *************/

Error = System.FormatEx ception: 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.Globaliz ation;

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.WriteLi ne (formatted);
}
}

--
Jon Skeet - <sk***@pobox.co m>
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

/************COD E***********/
using System;
using System.Globaliz ation;
public class Test
{
static void Main()
{
string original = "16/2/2006"; //could be in any format
CultureInfo other = new CultureInfo
(CultureInfo.Cu rrentCulture.Na me);
CultureInfo us = new CultureInfo ("en-US");
DateTime parsed = DateTime.Parse (original, other);
string formatted = parsed.ToString ("d", us);
Console.WriteLi ne (formatted);
}
}
/*************** *************** *******/

Jan 4 '06 #4
rs****@gmail.co m 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.Cu rrentCulture.Na me);


Why create a new CultureInfo rather than just using
CultureInfo.Cur rentCulture?

Jon

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

/************COD E***********/
using System;
using System.Globaliz ation;
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.Cur rentCulture);
string formatted = parsed.ToString ("d", us);
Console.WriteLi ne (formatted);
}
}
/*************** *************** *******/
rs****@gmail.co m
Jan 4, 10:17 am show options

Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
From: "rsa...@gmail.c om" <rsa...@gmail.c om> - 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
7260
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. Specifically, I have a problem with this portion in the WHERE clause: DATEADD(Day,tblMyEventTableName.ReminderDays, @DateNow) Between...
1
1787
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 would be identical. I was 100% incorrect. The code below produces the results: CType Took: 0.2187528 seconds. Convert Took: 12.187656 seconds.
4
3615
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 of the type the object is instantiated with. In my test program I have Option<std::string> and Option<long>. Here's the code for OptionBase and...
5
18516
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 tried the following and playing around with the following: declare @valuePointer varbinary(16)
7
7089
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 without strtol or s/printf function. Thanks, whatluo.
3
10265
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 and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps...
7
29200
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
1407
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 declaration statments (first lines) e.g. Public Function ConvertCurrencyToEnglish(ByVal MyNumber As Double) As String Private Function...
2
11991
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 convert(varchar,J1708Data.dtDateTime,120), count(convert(varchar,J1708Data.dtDateTime,120))
1
3588
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 some files and work on it. Let say, I want add new page to web project named websiteOrder.sln, i will open websiteOrder.sln in my local computer,...
0
7817
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8081
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8258
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8119
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5322
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3759
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3771
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2254
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.