467,074 Members | 883 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

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

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

I get errors on these lines:

dtGames.Rows[i]["playTime"] = (TimeSpan)dtGames.Rows[i]["playDate"];
dtGames.Rows[i]["playDay"] = (DayOfWeek)dtGames.Rows[i]["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[i]["playTime"] =
Convert.ToDateTime(dtGames.Rows[i]["playDate"]).TimeOfDay;
dtGames.Rows[i]["playDay"] =
Convert.ToDateTime(dtGames.Rows[i]["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[i]["playTime"] =
(TimeSpan)dtGames.Rows[i]["playDate"];
dtGames.Rows[i]["playDay"] =
(DayOfWeek)dtGames.Rows[i]["playDate"];
}

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

Nov 19 '05 #1
  • viewed: 1536
Share:

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Simple Simon | last post: by
15 posts views Thread by Fritz Switzer | last post: by
2 posts views Thread by Chris | last post: by
5 posts views Thread by ECVerify.com | last post: by
6 posts views Thread by Mike | last post: by
8 posts views Thread by ajs | last post: by
2 posts views Thread by David P. Donahue | last post: by
3 posts views Thread by =?Utf-8?B?U2hlbGRvbg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.