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

Discussion in 'ASP .Net' started by Harry Haller, Jul 21, 2005.

  1. Harry Haller

    Harry Haller Guest

    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;
    }
    Harry Haller, Jul 21, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    686
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    519
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    373
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    328
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    743
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page