Search code examples
c#datetimedataadapterdatacolumn

How to divide DATETIME column value into day, month and year in C#


I have a DATETIME column in my database. I need to get the value and divide it into 3 variables each having the year, month and day

I am trying to do

dataadapter.Fill(dataset, "tablename");
string x;  
foreach(DataRow dd in dataset.Tables["tablename"].Rows)
{
    x = dataset.Tables["tablename"].Rows[0]["date1"].ToString();
}

here x is holding the whole of m/dd/yr and time but how can I now have 3 variables which can store the month in a, year in b and day in another variable c?


Solution

  • DateTime dt = DateTime.Parse(x);
    int day = dt.Day;
    int month = dt.Month;
    int year = dt.Year;