473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.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
14 3370
<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
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**********@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
<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
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
<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
<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
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
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
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
Got it MM = month, mm = minutes.

Mar 21 '06 #12
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
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"?>...
8
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( _...
3
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...
12
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...
2
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...
9
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...
6
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
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") ...
3
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...
1
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 ( , , ,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.