none
Convert String Column To DateTime In DataTable RRS feed

  • Question

  • We are populating a DataTable like the below - taking a datetime field and populating it into a String.

    How should this code be edited to keep the DateTime value and insert into the DataTable?

    DataTable dtData = new DataTable();
    DataRow newRow = new DataRow();
    
    dtData.Columns.Add("HireDate", typeof(string));
    
    DateTime d1 = Convert.ToDateTime(listItem["Hire_Date_Main"]);
    if (String.Format("{0:MM/dd/yyyy}", date) != "1/1/0001")
        newRow["HireDate"] = d1;
    
    dtData.Rows.Add(newRow);

    Sunday, March 10, 2019 9:47 PM

Answers

  • Hi IndigoMontoya,

    Thank you for posting here.

    For your question, you could try the code below.

      // Here we create a DataTable with four columns.
                DataTable table = new DataTable();
                table.Columns.Add("Age", typeof(int));           
                table.Columns.Add("Name", typeof(string));
                table.Columns.Add("Date", typeof(string));
    
                // Here we add five DataRows.
                table.Rows.Add(25,  "David", DateTime.Now);
                table.Rows.Add(50,  "Sam", DateTime.Now);
                table.Rows.Add(10, "Christoff", DateTime.Now);
                table.Rows.Add(21,  "Janet", DateTime.Now);
                table.Rows.Add(100,  "Melanie", DateTime.Now);
    
                DataTable dtCloned = table.Clone();
                dtCloned.Columns[2].DataType = typeof(DateTime);
                foreach (DataRow row in table.Rows)
                {
                    dtCloned.ImportRow(row);
                }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 11, 2019 2:51 AM
    Moderator

All replies

  • So if you want a DateTime value to be stored as a string use one of the DateTime.ToStringXXX depending on the parts needed e.g. just date, date in a specific format in tangent or not with time and or not time specifier.

    Conceptual example.

    var dt = new DataTable();
    dt.Columns.Add(new DataColumn() {ColumnName = "HireDate", DataType = typeof(string)});
    object someDate = "03/6/2019";
    if (DateTime.TryParse(someDate.ToString(), out var date))
    {
        dt.Rows.Add(date.ToString(CultureInfo.InvariantCulture));
        Console.WriteLine();
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 10, 2019 11:06 PM
    Moderator
  • We are currently taking the datetime and converting it to string format.

    I want to KEEP it as a DateTime format, but unsure how to.

    Sunday, March 10, 2019 11:43 PM
  • We are currently taking the datetime and converting it to string format.

    I want to KEEP it as a DateTime format, but unsure how to.

    If you want to keep it as a DateTime you need to store the value as a DateTime and then when presenting the DateTime to the user interface that is when you format the value, not storing it as a string, that is totally wrong and should had mentioned this in my first reply.

    DateTime fields are not meant to be stored formatted period no matter what database is used.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 10, 2019 11:50 PM
    Moderator
  • Hi IndigoMontoya,

    Thank you for posting here.

    For your question, you could try the code below.

      // Here we create a DataTable with four columns.
                DataTable table = new DataTable();
                table.Columns.Add("Age", typeof(int));           
                table.Columns.Add("Name", typeof(string));
                table.Columns.Add("Date", typeof(string));
    
                // Here we add five DataRows.
                table.Rows.Add(25,  "David", DateTime.Now);
                table.Rows.Add(50,  "Sam", DateTime.Now);
                table.Rows.Add(10, "Christoff", DateTime.Now);
                table.Rows.Add(21,  "Janet", DateTime.Now);
                table.Rows.Add(100,  "Melanie", DateTime.Now);
    
                DataTable dtCloned = table.Clone();
                dtCloned.Columns[2].DataType = typeof(DateTime);
                foreach (DataRow row in table.Rows)
                {
                    dtCloned.ImportRow(row);
                }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 11, 2019 2:51 AM
    Moderator