473,659 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'String was not recognized as a valid DateTime'Error Occurs

11 New Member
Hi,
i have a string variable having date as following format "05-01-2010"
when convert this date into DateTime Format i got the following error 'String was not recognized as a valid DateTime'
Please Help me to solve it
Jan 5 '10 #1
3 4970
alexis4
113 New Member
I get from my pc a format like that:
5/1/2010 2:35:08 μμ
If I had English windows I suppose that instead of "μμ" I would get a "pm".
Jan 5 '10 #2
ThatThatGuy
449 Recognized Expert Contributor
string date = "05-01-2010";
Console.WriteLi ne(DateTime.Par se(date));

This works fine
Jan 5 '10 #3
Plater
7,872 Recognized Expert Expert
You will need to use ParseExact/TryParseExact, to supply the date format.
For example:
Expand|Select|Wrap|Line Numbers
  1. string StringWithDate = "05-01-2010";
  2. DateTime myDT = DateTime.MinValue;
  3. bool SuccessfullConversion=DateTime.TryParseExact(StringWithDate, "MM-dd-yyyy", null, System.Globalization.DateTimeStyles.None, out myDT);
  4. //if it returned true, myDT should now be set to correct date
  5.  
Try playing around with it some if need be.
Jan 5 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3236
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert value to a time value' error. It's related to glibc returning an error to a pre-1970 date, I think. My question: /how/ do I go through the Python direction I've created to remove the pre-1970 date objects? Ideally, I would be able to iterate...
5
3963
by: .Net Sports | last post by:
I have a datagrid codebehind script that takes data from sql dbase and displays it in a footer row as a total. One column has amount_dollars (which works fine), while another has new sales (which is "newstotal"), but I'm getting an error (System.InvalidCastException: Specified cast is not valid) from the newstotal line: price = (Decimal)(rowData); newstotal = (rowData); newstotal is an integer, just wondering what datatype i should use
3
3028
by: .Net Sports | last post by:
IN a datagrid code behind, I'm getting a "Specified cast is not valid" error price = (Decimal)(rowData); which was initialized as: Decimal price;
1
1844
by: amitbadgi | last post by:
Welcome back amitbadgi | Logout | Faq Knowledge Discovery Keys COMPUTER PROGRAMMING, DATA MINING, STATISTICS, ARTIFICIAL INTELLIGENCE * Settings * Photos * Lists * MVPs * Forums * Blogs
4
1826
by: redwebgeek | last post by:
Please help me. ANY guess or ideas welcome I am trying to list the types of coverpages available in winfax 10. I have the imports at the top of the page. Every time I run this I get a "Specified Cast is not valid" error. I guess this makes sense since the .count item is a short, but I have dimed x as short. Any ideas. please :roll: Dim x As Short Dim objWinfaxSend As New wfxctl32.COleCoverPages x = objWinfaxSend.Count()
14
7956
by: Me | last post by:
Hi all I am getting a really bizzare error on when I convert a string into a datetime: The code is : DateTime dt1 = Convert.ToDateTime("10 Sep 2005"); Console.WriteLine(dt1.Year);
9
4618
by: xian2 | last post by:
Hi All, I have been wroking on the databse for a while now and am on the stage of creating forms. I am trying to create a subform at the moment but am having some problems. I am using the wizard to create the form and add the necessary fields as below: The tables are as follows: tblTourBookings (Main Table linked to others)
5
2315
mafaisal
by: mafaisal | last post by:
Hi Everybody My Error when i Retrieve imge file from sql2005 database parameter not valid error is coming my code is Dim Logo As Byte() = RsC.Fields("Logo").Value Dim stream As System.IO.MemoryStream stream = New System.IO.MemoryStream(Logo)
0
1214
mafaisal
by: mafaisal | last post by:
Hi Everybody I am Using Vb.net 2005 My Error when i Retrieve imge file from sql2005 database parameter not valid error is coming my code is Dim Logo As Byte() = RsC.Fields("Logo").Value
0
8850
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
8746
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
8523
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
8626
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
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
4175
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...
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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.