Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am copying datarow from one table to another datatable column structrure are not same hence i use
DataTable dt = new DataTable();
C#
dt.Columns.Add("START DATE", typeof(System.DateTime));
 dt.Columns.Add("END DATE",typeof(System.DateTime));


DataRow row = dt.NewRow();

//copy row from old table
foreach (DataRow dr in WorksheetInfo.Rows)
{
row = dt.NewRow();
row[0] = dr["StartDate"].ToString();
row[1] = dr["EndDate"].ToString();
dt.Rows.Add(row);
}
dt.AcceptChanges();

i want date should be display in format 24/12/2012 12:00:00 its requirement of project can not change.

but now my current date showing in datable is 12/24/2012 only

I try everything example

1) string dtenddate = dr["EndDate"].ToString();
DateTime dtendate = Convert.ToDateTime(dtenddate);
row[1] = dtendate;

2)string dtdate = String.Format("{0 d/M/yyyy HH:mm:ss}",Convert.ToDateTime(dr["EndDate"]));
row[1] = dtdate;
any many more
last option i try for array split event if here it show error
C#
DateTime dtstart = Convert.ToDateTime(dr["EndDate"]);                       string[] strDateTime = dtstart.ToString("dd/MM/yyyy").Split('/');

                       // displaying values in textboxes

                     string  txtDate = strDateTime[0];
                     string  txtMonth = strDateTime[1];
                      string txtYear = strDateTime[2];


but it showing error as string was not recognise as valid date time,anyone have idea how to get my requirement
Posted
Comments
Karthik_Mahalingam 22-Jan-14 9:15am    
what you are getting in this dr["StartDate"].ToString(); ???
invisible@123 23-Jan-14 1:56am    
DateTime dtstartsdate = DateTime.ParseExact(dr["EndDate""].ToString(), "MM/dd/yyyy", CultureInfo.InvariantCulture);

row[1] = dtstartsdate;

suppose dr["EndDate"] contain value 11/14/2013 it work fine but

dr["EndDate"] contain 6/8/2010 gives u error as String was not recognized as a valid DateTime if i change 06/08/2010 it will work fine but i dont know how to change date from 6/8/2010 to 06/08/2010 anybody have idea?

Never use Convert.ToDateTime, but rather DateTime.TryParseExact.
Next you can modify the format of a string represenation of a DateTime object by providing the format string in the tostring method:
C#
DateTime mydts = DateTime.Now;
string mystringdts = mydts.ToString("dd/MM/yyyy HH:mm:ss");


see here[^]
and here[^]

hope this helps.

PS: Do not see a datetime as something you see (like 26/04/2005 for April 26th 2005) it is an object capable of being represented in numerous ways. Therefore, unless you SPECIFY the format it needs to be, it will take the form in a very machine dependent way. (cultural settings)
 
Share this answer
 
Comments
BillWoodruff 22-Jan-14 14:21pm    
+5
V. 22-Jan-14 15:00pm    
:-)
invisible@123 23-Jan-14 0:29am    
.DateTime mydts = (DateTime)dr["EndDate"];
row[1] = mydts.ToString("dd/MM/yyyy HH:mm:ss");

Still it showing Error
V. 23-Jan-14 2:08am    
I'm "guessing" you're still confused with DateTime and string. What is the error you get?
On which line?
DateTime mydts = (DateTime)dr["EndDate"];
or
row[1] = mydts.ToString("dd/MM/yyyy HH:mm:ss");

The safest (not necessarely the best) way to work here is:
if(dr["enddate"] != DBNull.Value){ //check if you didn´t receive a NULL value from the database (Which doesn´t equal the null from C#!)
string str_enddate = Convert.ToString(dr["EndDate"]);
if(!string.IsNullOrEmpty(str_enddate)){
DateTime enddts;
bool success = DateTime.TryParseExact(/*don´t know the exact arguments by heart last one is "out enddts"*/);
if(success){
row[1] = enddts; //since you defined it as typeof(System.DateTime)
}
}
}
apologies for the lack of formatting, not possible in comments.
When you say the date shows in your datatable in the wrong format - how are you looking at it?

As mentioned, how you see it is entirely dependent on what you are using to look at it. If you are using some tool to look at the data table then that tool will decide how to display the date - it is always stored the same internally - it is a date, not a string representation of a date.

In the database the date is stored as a binary representation, and the act of displaying it requires some translation - which if not specified by you will probably use your machine's current culture to determine the expected format.

If your date is being displayed in a format you don't want then, assuming whatever tool you are using to display it is using a system value, you will have to modify the OS on which it is running.

e.g. the default date format for US machines is probably MM/DD/YYYY but UK is DD/MM/YYYY
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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