Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi,
 
I am getting an error of string converion to datetime.
 
strdate comes dynamically..for e.g: strdate11=24-06-12
dtDate12 = Convert.ToDateTime(strdate11);
the above was working in my localhost; but gave error on staging site.
 
So, I wrote the conversion format in the folloeing way:
dtDate12 = DateTime.ParseExact(strdate11, "MM/dd/yyyy HH:mm:ss", null);
and also the last attribute to CultureInfo.InvariantCulture;
 
But it gives me eror on my localhost itself:
String was not recognized as a valid DateTime
 
Please help
 
Thanks,
Kiran
Posted 9-Jul-12 1:57am
v2
Comments
Wes Aday at 9-Jul-12 9:09am
   
Additionally, I think that you will probably find that "12" is not a valid year.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is due to the difference in culture settings on your local machine and hosting server. You need to specify the culture in your web.config which will override the hosting server settings
 
http://stackoverflow.com/questions/3768061/datetime-format-different-on-local-machine-compared-to-production-machine[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

try This :
 
   IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB", true);
        DateTime from_date = DateTime.ParseExact(string1, "dd/MM/yyyy", theCultureInfo);
        DateTime to = DateTime.ParseExact(string2, "dd/MM/yyyy", theCultureInfo);
        
this will convert your string into Datetime Object.
 
Hope this will help you.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this...its working
string dateString = "01-01-2012";
        DateTime date1 = new DateTime();
        date1 = DateTime.Parse(dateString, new CultureInfo("en-GB").DateTimeFormat);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

hi
Check this[^]
 
try this[^]
may be this will help you.
 
Thank You
ChetanV
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Change this (year format change)
dtDate12 = DateTime.ParseExact(strdate11, "MM/dd/yy HH:mm:ss", null);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

you can also use :-
Quote:
DateTime dtEx = DateTime.Parse(str);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

If the string is questionable do a try parse to test before you parse the data from the string, you should be able to reuse the original code if the try parse returns the correct value else you need to format your date string correctly.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 741
1 OriginalGriff 255
2 Volynsky Alex 205
3 Kishore Pogaru 130
4 Magic Wonder 129
0 OriginalGriff 5,705
1 Sergey Alexandrovich Kryukov 4,763
2 CPallini 4,570
3 George Jonsson 3,142
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100