473,763 Members | 3,712 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
14 3403
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.Globaliz ation;

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

CultureInfo ci = new CultureInfo("en-US",true);
ci.DateTimeForm at.ShortDatePat tern = "dd.mm.yy";

myStartDate = DateTime.Parse( stringDate, ci);

Console.WriteLi ne("Successful. ");

}

}

Mar 21 '06 #11
Got it MM = month, mm = minutes.

Mar 21 '06 #12
er**********@gm ail.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.ParseE xact 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**********@gm ail.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.ParseE xact 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**@discussio ns.microsoft.co m> 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.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 22 '06 #15

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

Similar topics

4
1987
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
12430
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
2607
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
7069
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
34099
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
11183
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
7162
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
1534
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
9563
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
10145
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...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6642
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
5270
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
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.