Hello,
A DataTable is for storing data, not for changing the format in the case of a DateTime, no different from storing a DateTime in a database table.
If displaying in say a DataGridView, set the format.
Code
public Form1()
{
InitializeComponent();
Shown += OnShown;
dataGridView1.AutoGenerateColumns = false;
}
private void OnShown(object sender, EventArgs e)
{
var table = new DataTable();
table.Columns.Add(new DataColumn("SomeDate", typeof(DateTime)));
table.Rows.Add(new DateTime(2021,1,1));
table.Rows.Add(new DateTime(2021,2,1));
table.Rows.Add(new DateTime(2021,3,1));
dataGridView1.DataSource = table;
}
Then we can do the following
private void button1_Click(object sender, EventArgs e)
{
var table = (DataTable) dataGridView1.DataSource;
StringBuilder sb = new StringBuilder();
foreach (DataRow row in table.Rows)
{
sb.AppendLine(row.Field<DateTime>("SomeDate")
.ToString("dd-MMM-yy"));
}
MessageBox.Show(sb.ToString());
}
Or
var datesFormatted = ((DataTable) dataGridView1.DataSource)
.AsEnumerable()
.Select(row => row.Field<DateTime>("SomeDate").ToString("dd-MMM-yy"))
.ToArray();
MessageBox.Show(string.Join("\n",datesFormatted));
DateTime.Now