473,748 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting string to date time

Hello,

I'm trying to convert a string to a date time in a C# web service.

I'm passing in a string parameter and I have a localization setting in
my Web.config file:

My app is blowing up on the first line here, with a"Value does not fall
within the expected range" error.

CultureInfo ci = new CultureInfo("en-US",true);
ci.DateTimeForm at.ShortDatePat tern =
ConfigurationSe ttings.AppSetti ngs["DateFormat "];

try
{
DateTime myStartDate = DateTime.Parse( startDate, ci);
}

Any idea what's wrong? Is there a simpler way to do this?

-Eric

Mar 20 '06 #1
14 3400
<er**********@g mail.com> wrote:
I'm trying to convert a string to a date time in a
C# web service. [...] Any idea what's wrong?
Well, what's the value that you're passing in?
Is there a simpler way to do this?


Why can't you pass in a DateTime instead of a string? It also won't
fail when you have to deal with locales outside the United States
(e.g. Europe and Japan, which each have their own different date
formats).

Eq.
Mar 20 '06 #2
Hello,

My web service is a test harness to mimic the behavior of another
company's web service, so I have to use the API that's they use.

I don't think that it matters what I pass through, I'm passing in
"01.01.06". My app is giving me the Value does not fall
within the expected range" when I try to execute the first line:

CultureInfo ci = new CultureInfo("en-US",true);

Thanks,
-Eric

Mar 20 '06 #3
<er**********@g mail.com> wrote:
My web service is a test harness to mimic the behavior of another
company's web service, so I have to use the API that's they use.

I don't think that it matters what I pass through, I'm passing in
"01.01.06". My app is giving me the Value does not fall
within the expected range" when I try to execute the first line:

CultureInfo ci = new CultureInfo("en-US",true);


That seems somewhat unlikely. What does the full stack trace look like?
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
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
Mar 20 '06 #4
<er**********@g mail.com> wrote:
I don't think that it matters what I pass through, I'm passing in
"01.01.06". My app is giving me the Value does not fall within
the expected range" when I try to execute the first line:

CultureInfo ci = new CultureInfo("en-US",true);

Sorry - read it too quickly.

"Value does not fall within the expected range" is the default message
for ArgumentExcepti on, which the CultureInfo constructor can throw,
but only if "<name> is not a valid culture name" - and your line of
code with "en-US" works fine on my machine here. I don't think it's
possible to make it fail by somehow adding or removing cultures,
because they're a standard set, regardless of what international
support the particular computer has got.

How are you determining where the error is? Do you have the complete
stack trace available? I've sometimes seen the debugger indicate the
wrong line (one above or below), so it *might* not be that first line.

Eq.
Mar 20 '06 #5
I get the same thing when I try to execute the same line in a simple
Winforms application, here is the code for my Form1:

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Globaliz ation;

namespace WindowsApplicat ion2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
// TODO: Add any constructor code after InitializeCompo nent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
CultureInfo ci = new CultureInfo("en-US",true);

}
}
}

Mar 20 '06 #6
<er**********@g mail.com> wrote:
I get the same thing when I try to execute the same
line in a simple Winforms application, here is the code
for my Form1:
[... snipped example program ...]


I copied and pasted that exactly, and checked that it was reaching the
CultureInfo creation line, and it ran perfectly. There must be some
strange configuration problem at your end. Short of running it on a
different machine - or removing and reinstalling the Framework (which,
of course, isn't guaranteed to fix it) - I don't know what to suggest,
unfortunately.

Eq.
Mar 20 '06 #7
<er**********@g mail.com> wrote:
I get the same thing when I try to execute the same line in a simple
Winforms application, here is the code for my Form1:


Just a hint for future examples - console apps make much simpler little
test apps. Does the following work on your computer?

using System;
using System.Globaliz ation;

class Test
{
static void Main()
{
Console.WriteLi ne(new CultureInfo("en-US",true));
}
}

--
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
Mar 21 '06 #8
Strangely your sample app does work. When I comment out your line and
put in CultureInfo ci = new CultureInfo("en-US",true); it doesn't
work.

-Eric

using System;
using System.Globaliz ation;

class Test
{
static void Main()
{
//Console.WriteLi ne(new CultureInfo("en-US",true));
CultureInfo ci = new CultureInfo("en-US",true);

}

}

Mar 21 '06 #9
Weird weird weird. If I copy and past your "new
CultureInfo("en-US",true)" to the right of my "=" sign it does work.
If I do a Ctrl-Z and step back though my code it doesn't. I guess
there's some kind of weird character in there or something?

This works:

using System;
using System.Globaliz ation;

class Test
{
static void Main()
{
//Console.WriteLi ne(new CultureInfo("en-US",true));
CultureInfo ci = new CultureInfo("en-US",true);

}

}

This does NOT work:

using System;
using System.Globaliz ation;

class Test
{
static void Main()
{
//Console.WriteLi ne(new CultureInfo("en-US",true));
CultureInfo ci = new CultureInfo("en-US",true);

}

}

Mar 21 '06 #10

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

Similar topics

4
1986
by: Tobias Müller | last post by:
Hello everybody, I've got some weather data from my local wx station and want to display this as a short table by using a XSL template. The data looks like <?xml version="1.0"?> <weather-data> <station name="external temp." sensor="3" type="temperature" unit="°C"> <datum date="04.09.2005" time="12:00" value="10" />
8
2488
by: Mika M | last post by:
Is there better way to convert integer type date into DateTime type date as doing like code below? Dim intDate As Integer = 20051019 Dim dte As DateTime = New DateTime( _ CType(intDate.ToString.Substring(0, 4), Integer), _ CType(intDate.ToString.Substring(4, 2), Integer), _ CType(intDate.ToString.Substring(6, 2), Integer))
3
12426
by: NateM | last post by:
How do I convert any given date into a milliseconds value that represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT? Is there an easy way to do this like Date in java? Thanks, Nate
12
2606
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for example: 21/05/2006
2
7068
by: TofuTheGreat | last post by:
I'm using "Now.ToOADate" for a record timestamp in a small database app (it's what I want to do so don't try to disuade me ;-D). Anyway. I store the value of Now.ToOADate in a string field in the database (16 character field on a SQL server to be exact). Is there a way of converting the text value from that field back into a readable date/time? For example if a date is stored in the 16 character field as the OLE Automation format...
9
12945
by: Alok yadav | last post by:
i am using a webservice in which a method is serach. i use this method which accept a argument of date type in dd/MM/yyyy formate. i have a textbox which accept the date from the user, when i convert textbox data into Datatime formate it converted into MM/dd/yyyy formate, but i have a requirement in dd/MM/yyyy formate. please help me, i am using c#.
6
34096
by: marc | last post by:
hi im trying to convert Date() into a unix timestamp so i can stick the result into a mysql db, please help!
2
11181
by: Brian Parker | last post by:
I am beginning to work with VB2005.NET and I'm getting some problems with string formatting converting an application from VB6. VB6 code:- sTradeDate = Format(pArray(4,i Record), "mmddyy") pArray is a variant array containing a date string at pArray(4, iRecord) in the format "yyyy/mm/dd"
3
7159
by: Jef Driesen | last post by:
How can I convert a date string to a number (e.g. a time_t value or a tm struct)? I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying to autoformat the contents of text entries in a GUI. For numbers, I'm converting the text representation to the appropriate type (using atoi, atof, ...) and converting the result back to text with the correct format (using sprintf). But this does...
1
1533
by: rob41 | last post by:
I'm in the process of converting numerous queries from access 07 to sql server 05 to improve runtime performance. Below is a sample of code and the error I'm getting. INSERT INTO ( , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ) SELECT ., ., ., LTrim(.) AS Expr1, LTrim(.) AS , LTrim(.) AS , LTrim(.) AS , LTrim(.) AS , LTrim(.) AS , LTrim(.) AS , LTrim(.) AS , LTrim(.) AS ,...
0
8984
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
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
2
2775
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.