tags:

views:

643

answers:

3

hi guys....

   objfile.dateFileDate=convert.ToDatetime(Format(txtdate.text,"MM/dd/yyyy hh:mm"))

following error is coming

The string was not recognized as a valid datetime .There is an unknown word starting at 0.

What should i do to save this datetime, please help

+4  A: 

You can't format normal text using datetime formats.

Try

C#

objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", null);

VB.NET

objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", Nothing)

This is assuming dateFileDate is a DateTime type and that the txtdate.text is in the above format.

Mladen Mihajlovic
I believe he is using VB.Net - and Format is a legacy (backwards compatible) VB6 method that would format his string as he is requesting.
Jeff Olson
Even in VB6 you can't use Format to format an arbitary string. You would need to supply a date type to be able to format using that format string.
Mladen Mihajlovic
Just to mention, Convert.ToDateTime is not wrong and can also be used, but DateTime.ParseExact allow you to specify a format explicitly.
Mladen Mihajlovic
@Mladen: Good solution just a typo, above "Nothing" is for vb.net and "null" is for c#
Raghav Khunger
Thanks @Raghav Khunger - fixed.
Mladen Mihajlovic
A: 

Try hh:nn instead of hh:mm

I believe mm is Months in two digit format and nn is minutes in two digit format.

Jeff Olson
mm is minutes, MM is months
Mladen Mihajlovic
See here for reference: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Mladen Mihajlovic
+1  A: 

If your program is used by a international crowd, read on :)

ppl from different cultures will write dates in diffrent formats, so if your always going to parse the string that could be get sticky. Consider using the calander control? Im saying this based on personal experience. Also finding out why your current one is failing, i would do a DateTime.Now.ToString() and compare that to whats in the textbox so you can see whats curently being typed in wrong ( While debugging off course, to help track down the problem)

EKS