Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hai,

I need to convert string to datetime.I have used:

DateTime date = Convert.ToDateTime(txtdate.Text);

I need to take the month from the date.

But i Got the error:

String was not recognized as a valid DateTime

Can any one pls tell the solution.Thanks in Advance
Posted
Comments
Member 13265360 12-Oct-17 6:52am    
An exception of type 'System.FormatException' occurred in System.Data.dll but was not handled in user code

Additional information: Failed to convert parameter value from a String to a DateTime.

Well, it means the string cannot be parsed as System.DateTime. You need to specify expected format. The right approach is this:

http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx[^],
http://msdn.microsoft.com/en-us/library/9h21f14e.aspx[^],
http://msdn.microsoft.com/en-us/library/ms131044.aspx[^],
http://msdn.microsoft.com/en-us/library/h9b85w22.aspx[^].

In a nutshell: you either create and instance of CultureInfo to use as IFormatProvider (which is implemented by CultureInfo) or explicitly specify exact format using a format string. You will find it all from the MSDN articles referenced above.

—SA
 
Share this answer
 
Comments
Rajeev Jayaram 2-Feb-12 4:17am    
5!
Sergey Alexandrovich Kryukov 2-Feb-12 4:19am    
Thank you, Rajeev.
--SA
ssaakk 15-Jul-12 23:44pm    
thank you
Check txtdate.Text format,Is it a valid C# datatime format.
and you can use as
C#
DateTime date = DateTime.ParseExact(txtdate.Text, "dd/MM/yyyy", null);
 
Share this answer
 
v2
To take month value from the date
You can use SubString property of the text.
ie if text is in the format dd-mm-yyyy the
txtdate.Text.SubString(2,2)
will give you the Month.
Happy Programming:-)
 
Share this answer
 
You can try this link:
http://forums.asp.net/t/1726067.aspx[^]
 
Share this answer
 
Spot on reference to convert String to a DateTime,

http://msdn.microsoft.com/en-us/library/cc165448.aspx[^]

Hope it helps.
 
Share this answer
 
Hi ,

use can use it
txtToDate.Text = "4/23/2013"

DateTime date = DateTime.ParseExact(txtToDate.Text, "M/d/yyyy", null);
date return {23/04/2013 00:00:00}

txtfromDate_new.Text = date.ToShortDateString();

Out Put is
txtfromDate_new.Text = "23/04/2013"

if you wants date format in
dd/MM/YYYY format
Use
txtToDate.Text="23/4/2013"

teTime date = DateTime.ParseExact(txtToDate.Text, "d/M/yyyy", null);

txtfromDate_new.Text = date.ToShortDateString();

after getting datetime object You can Convert datetime in any format

string.Format("{0:d/M/yyyy}", date);----23/4/2013
string.Format("{0:M/d/yyyy}", date);----4/23/2013
 
Share this answer
 
v2
Copying one gridview to another gridview data dynamically in asp.net C#
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt_vilas = new DataTable();
//dt_Grd.Columns.Add("name", typeof(string));
for (int i = 0; i < GrdLeaveDetail.Columns.Count; i++)
{
//dt_Grd.Columns.Add(GrdLeaveDetail.Columns[i].ToString(), GrdLeaveDetail.Columns[i].GetType());
dt_vilas.Columns.Add(GrdLeaveDetail.Columns[i].ToString(), typeof(string));
}

int k = 1;
foreach (GridViewRow gvr in GrdLeaveDetail.Rows)
{
dt_Grd.NewRow();
// Ds.Tables[0].Columns[0] = "";
//Ds.tables[0].rows[k]["lname"] = GrdLeaveDetail.Rows[k]["lname"];
DataRow dtrow = dt_vilas.NewRow(); // Create New Row
//dtrow["Empid"] = gvr.Cells[1].Text.ToString();
dtrow[dt_vilas.Columns[0].ColumnName.ToString()] = gvr.Cells[0].Text.ToString();
dtrow[dt_vilas.Columns[1].ColumnName.ToString()] = gvr.Cells[1].Text.ToString();
dtrow[dt_vilas.Columns[2].ColumnName.ToString()] = gvr.Cells[2].Text.ToString();
dtrow[dt_vilas.Columns[3].ColumnName.ToString()] = gvr.Cells[3].Text.ToString();
dtrow[dt_vilas.Columns[4].ColumnName.ToString()] = gvr.Cells[4].Text.ToString();
dtrow[dt_vilas.Columns[5].ColumnName.ToString()] = gvr.Cells[5].Text.ToString();
dtrow[dt_vilas.Columns[6].ColumnName.ToString()] = gvr.Cells[6].Text.ToString();
dtrow[dt_vilas.Columns[7].ColumnName.ToString()] = gvr.Cells[7].Text.ToString();
//dtrow["Leave_From_Date"] = gvr.Cells[2].Text.ToString();
//dtrow["Leave_To_Date"] = gvr.Cells[3].Text.ToString();
//dtrow["Reason"] = gvr.Cells[4].Text.ToString();
dt_vilas.Rows.Add(dtrow);
}

GridView2.DataSource = dt_vilas;
GridView2.DataBind();

}
 
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