Click here to Skip to main content
11,647,810 members (61,669 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi,

I have a problem while converting date which is entered by user in format of 'MM-dd-yyyy'.
I used
dt = convert.todatetime(txt_date.text);

but it shows error as String was not recognized as a valid DateTime.

Eg. I want to convert date "06-16-2011" as datetime using convert.todatetime.
Posted 15-May-11 20:47pm
Edited 15-May-11 23:48pm
ambarishtv10.5K
v4
Comments
Dalek Dave at 16-May-11 4:16am
   
Edited for Grammar and Readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

try
 {
 
  CultureInfo format_dt = new CultureInfo(en-US);
  dt = DateTime.ParseExact(txt_date.Text, "MM-dd-yyyy", format_dt)
 }
 catch (FormatException)
 {
  Messagebox.show(FormatException.show);
}

This is working
  Permalink  
v2
Comments
ambarishtv at 16-May-11 4:46am
   
my 5 for "FormatException" handling :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you want to convert a string to a date time, and it is going to always be in a specific format, rather than the culture of the PC it is running on, then use the DateTime.ParseExact[^] Method:
dt = Date.ParseExact("05-16-2011","dd-MM-yyyy", CultureInfo.InvariantCulture)
  Permalink  
Comments
Dalek Dave at 16-May-11 4:17am
   
This is true.
ambarishtv at 16-May-11 4:43am
   
hi,
the given syntax is not valid one.
how to manage when txt_date.text is invalid date format.

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

dt = convert.todatetime(txt_date.text.ToString("MM-dd-yyyy"));
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

DateTime date= DateTime.ParseExact(dtText.Text, "MM-dd-yyyy", null);


Use it .

It may help you
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

DateTime.TryParseExact() is the best way to convert string with custom format to DateTimedatatype

string myDate = "06-16-2011";
            DateTime dt;
             
            //verify given date is valid or not
            if (DateTime.TryParseExact(myDate, "dd-mm-yyyy", CultureInfo.InvariantCulture,     System.Globalization.DateTimeStyles.None, out dt))
            {
               //

            }
            else
            {
             //invalid date format
            }

link
http://msdn.microsoft.com/en-us/library/h9b85w22.aspx[^]
  Permalink  
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try this

//check for one more Thing TextBox1.Text should not be empty before converting
string dt = TextBox1.Text;
DateTime k = Convert.ToDateTime(dt);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

try

string dtText = TextBox1.Text;
Label1.Text = Convert.ToDateTime(dtText).ToString();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

You can use DateTime.Parse or DateTime.ParseExact instead of Convert.ToDate

Try out below link,

[link]
  Permalink  

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

  Print Answers RSS
0 jyo.net 500
1 F-ES Sitecore 410
2 DamithSL 290
3 CPallini 290
4 OriginalGriff 277
0 OriginalGriff 1,342
1 jyo.net 994
2 DamithSL 971
3 Sergey Alexandrovich Kryukov 873
4 CPallini 845


Advertise | Privacy | Mobile
Web01 | 2.8.150804.4 | Last Updated 16 May 2011
Copyright © CodeProject, 1999-2015
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