How do I convert a DateTime table column to a TimeSpan or DayOfWeek ?

H

Harry Haller

The context is shown below in the getGames() method.

I get errors on these lines:

dtGames.Rows["playTime"] = (TimeSpan)dtGames.Rows["playDate"];
dtGames.Rows["playDay"] = (DayOfWeek)dtGames.Rows["playDate"];

because the playDate column is a DateTime.

Here is my solution but I don't like it. What else can I do?

dtGames.Rows["playTime"] =
Convert.ToDateTime(dtGames.Rows["playDate"]).TimeOfDay;
dtGames.Rows["playDay"] =
Convert.ToDateTime(dtGames.Rows["playDate"]).DayOfWeek;


public DataView getGames()
{
DataTable dtGames = DAL.GetTable("Games");
DataColumn colItem;

colItem = new DataColumn("playTime",
Type.GetType("System.TimeSpan"));
dtGames.Columns.Add(colItem);

colItem = new DataColumn("playDay",
Type.GetType("System.DayOfWeek"));
dtGames.Columns.Add(colItem);

for (int i = 0; i <= dtGames.Rows.Count - 1; i++)
{
dtGames.Rows["playTime"] =
(TimeSpan)dtGames.Rows["playDate"];
dtGames.Rows["playDay"] =
(DayOfWeek)dtGames.Rows["playDate"];
}

dvwGames = dtGames.DefaultView;
Cache.Insert("Games", dvwGames);
return dvwGames;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top