By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,665 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,665 IT Pros & Developers. It's quick & easy.

Converting string to date time

P: n/a
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.DateTimeFormat.ShortDatePattern =
ConfigurationSettings.AppSettings["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
Share this Question
Share on Google+
14 Replies


P: n/a
<er**********@gmail.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

P: n/a
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

P: n/a
<er**********@gmail.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.com>
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

P: n/a
<er**********@gmail.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 ArgumentException, 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

P: n/a
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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Globalization;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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 InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(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(object sender, System.EventArgs e)
{
CultureInfo ci = new CultureInfo("en-US",true);

}
}
}

Mar 20 '06 #6

P: n/a
<er**********@gmail.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

P: n/a
<er**********@gmail.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.Globalization;

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

--
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
Mar 21 '06 #8

P: n/a
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.Globalization;

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

}

}

Mar 21 '06 #9

P: n/a
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.Globalization;

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

}

}

This does NOT work:

using System;
using System.Globalization;

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

}

}

Mar 21 '06 #10

P: n/a
I'm getting past the line that was blowing up before, apparently some
weird character in there. I copied the line of code off a website and
changed it to my "en-US", maybe there was some kind of weird character
in there. I tried to copy and paste through notepad to filter them
out, but it didn't help. Usually that will work to get plain text for,
say, Word documents. Now I'm getting a "Could not determine the order
of year, month, and date from dd.mm.yy. When I try to DateTime.Parse.
All this culture info stuff seems nice if your customers use the
default date format for their culture, but the old-fashioned Format
seems much easier for customization.

-Eric

using System;
using System.Globalization;

class Test
{
static void Main()
{
String stringDate = "01.01.06";
DateTime myStartDate;

CultureInfo ci = new CultureInfo("en-US",true);
ci.DateTimeFormat.ShortDatePattern = "dd.mm.yy";

myStartDate = DateTime.Parse(stringDate, ci);

Console.WriteLine("Successful.");

}

}

Mar 21 '06 #11

P: n/a
Got it MM = month, mm = minutes.

Mar 21 '06 #12

P: n/a
er**********@gmail.com wrote:
I'm getting past the line that was blowing up before, apparently some
weird character in there. I copied the line of code off a website and
changed it to my "en-US", maybe there was some kind of weird character
in there. I tried to copy and paste through notepad to filter them
out, but it didn't help. Usually that will work to get plain text for,
say, Word documents. Now I'm getting a "Could not determine the order
of year, month, and date from dd.mm.yy. When I try to DateTime.Parse.
All this culture info stuff seems nice if your customers use the
default date format for their culture, but the old-fashioned Format
seems much easier for customization.


If you need to parse a specific format, look at DateTime.ParseExact and
specify the format string yourself. You still need to give a culture
info, but in many cases the format string can be culture-insensitive
anyway.

Jon

Mar 21 '06 #13

P: n/a
I found that when I'm sending a query or inserting a record that has a date
which I grab from a textbox i've had to do the followin

"#" & datetextbox & "#"

I was getting the same error, SQL likes the pound signs on each end of the
date.

John
"Jon Skeet [C# MVP]" wrote:
er**********@gmail.com wrote:
I'm getting past the line that was blowing up before, apparently some
weird character in there. I copied the line of code off a website and
changed it to my "en-US", maybe there was some kind of weird character
in there. I tried to copy and paste through notepad to filter them
out, but it didn't help. Usually that will work to get plain text for,
say, Word documents. Now I'm getting a "Could not determine the order
of year, month, and date from dd.mm.yy. When I try to DateTime.Parse.
All this culture info stuff seems nice if your customers use the
default date format for their culture, but the old-fashioned Format
seems much easier for customization.


If you need to parse a specific format, look at DateTime.ParseExact and
specify the format string yourself. You still need to give a culture
info, but in many cases the format string can be culture-insensitive
anyway.

Jon

Mar 22 '06 #14

P: n/a
Vear <Ve**@discussions.microsoft.com> wrote:
I found that when I'm sending a query or inserting a record that has a date
which I grab from a textbox i've had to do the followin

"#" & datetextbox & "#"

I was getting the same error, SQL likes the pound signs on each end of the
date.


That's definitely not the right way to do it - you've opened yourself
up for a SQL injection attack. You should parse the string into a
DateTime, and then use a parameterized SQL statement, setting the
parameter values appropriately.

--
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
Mar 22 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.