Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I'm having issue converting string to DateTime. format of the string is

Jun 23 2017  4:46PM


So I should convert it to DateTime so I could compare it with
DateTime.Now



Thanks in advance!

What I have tried:

Take one:

var myDate = DateTime.ParseExact(viewModel.start, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);


Take two:
C#
var date = DateTime.Parse(viewModel.start)


Both gave me:
System.FormatException: 'String was not recognized as a valid DateTime.'


Detailed:
System.FormatException occurred
  HResult=0x80131537
  Message=String was not recognized as a valid DateTime.
  Source=mscorlib
  StackTrace:
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.DateTime.Parse(String s)
   at SmartAgro.Controllers.DashboardController.PostEvent(CalendarEventViewModel viewModel) in C:\Users\Faris\Documents\Visual Studio 2017\Projects\SmartAgro\SmartAgro\Controllers\DashboardController.cs:line 363
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
Posted
Updated 26-Jun-17 23:11pm
v2
Comments
Tomas Takac 27-Jun-17 5:04am    
Try this format: "MMM dd yyyy h:mmtt"
But the extra space there seems to be causing problems.

How does "Jun 23 2017 4:46PM" equate to "MM/dd/yyyy hh:mm:ss tt"? You are telling .net that the date looks like "06/23/2017 04:46:00 PM" yet you're giving it "Jun 23 2017 4:46PM" to parse. The format you give needs to match your date string exactly, the clue is in the method name :)

var myDate = DateTime.ParseExact("Jun 23 2017  4:46PM", "MMM dd yyyy  h:mmtt", CultureInfo.InvariantCulture);


Note I have catered for the double space between the year and the time, I don't know if that was a copy\paste error, if that space does not exist in reality then remove it from the format string also

var myDate = DateTime.ParseExact("Jun 23 2017 4:46PM", "MMM dd yyyy h:mmtt", CultureInfo.InvariantCulture);
 
Share this answer
 
Comments
Thanks7872 27-Jun-17 5:12am    
Straight to the point. +5
Faris Karcic 27-Jun-17 5:22am    
Yeah thats it! A beginners mistake.....
However I had to do some alteration cause I noticed on debugger it has changed format, so modification had to be done.

Thanks alot! Appreciate it!
F-ES Sitecore 27-Jun-17 5:33am    
If you're talking about what the myDate variable looks like in the debugger that's because dates don't carry a format, they are simply a representation of a point in time. When you show the variable by hovering over it or doing a Debug.Write(myDate) then to show it on the screen it needs to be converted to a string so it calls myDate.ToString which converts the date to a string based on an arbitrary format, generally what is defined in your regional settings of the OS.
Solution posted above is pretty much clear in order to overcome the issue. Apart from that,i'd suggest you to refer Custom Date and Time Format Strings | Microsoft Docs[^] to get idea of how to work with such problems.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900