Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: DateTime string
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 1-Feb-12 22:57pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
Rajeev Jayaram at 2-Feb-12 4:17am
   
5!
SAKryukov at 2-Feb-12 4:19am
   
Thank you, Rajeev.
--SA
ssaakk at 15-Jul-12 23:44pm
   
thank you
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Check txtdate.Text format,Is it a valid C# datatime format.
and you can use as
DateTime date = DateTime.ParseExact(txtdate.Text, "dd/MM/yyyy", null);
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 ProgrammingSmile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

You can try this link:
http://forums.asp.net/t/1726067.aspx[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Spot on reference to convert String to a DateTime,
 
http://msdn.microsoft.com/en-us/library/cc165448.aspx[^]
 
Hope it helps.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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("{0Big Grin | :-D /M/yyyy}", date);----23/4/2013
string.Format("{0:M/d/yyyy}", date);----4/23/2013
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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();
 
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 BillWoodruff 220
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,817
2 DamithSL 5,461
3 Manas Bhardwaj 4,946
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 9 Oct 2013
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