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. Advertisements

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:
    1,048
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. mp
    Replies:
    1
    Views:
    680
    John Machin
    Jul 28, 2006
  3. Steve Kershaw
    Replies:
    0
    Views:
    10,975
    Steve Kershaw
    Aug 26, 2006
  4. Steve Kershaw
    Replies:
    5
    Views:
    6,187
    Steve Kershaw
    Aug 29, 2006
  5. Martin
    Replies:
    0
    Views:
    646
    Martin
    Dec 27, 2008
  6. Replies:
    2
    Views:
    1,063
    M.-A. Lemburg
    Jan 6, 2009
  7. NiklasRTZ

    datetime.datetime. or datetime. ?

    NiklasRTZ, Oct 8, 2009, in forum: Python
    Replies:
    10
    Views:
    760
    niklasr
    Oct 11, 2009
  8. Justin Voelker
    Replies:
    2
    Views:
    480
    Dr J R Stockton
    Dec 2, 2008
Loading...