How do I subtract 1 hour from DATETIME string using timespan instance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JonDavid
    New Member
    • Nov 2009
    • 2

    How do I subtract 1 hour from DATETIME string using timespan instance

    CODE:
    DateTime coursestatusdat e = DateTime.MinVal ue;
    if (courseRow["coursestatusda te"].ToString() != string.Empty)
    {
    coursestatusdat e = DateTime.Parse( courseRow
    ["coursestatusda te"].ToString());
    //TODO: Convert coursestatusdat e to CST
    DateTime courseStatusDat eCentralTime =
    coursestatusdat e.Add(System.Ti meSpan.FromHour s(-1));
    }

    I need to preserve the time for coursestatusdat e, and I need courseStatusDat eCentralTime to reflect : coursestatusdat e - 1 hour
  • JonDavid
    New Member
    • Nov 2009
    • 2

    #2
    I found the problem

    I was almost there. All I had to do was uncomment my declaration, and removed the DATE TIME from my conversion. ...all is well.

    private void Update_Course(D ataRowView courseRow, DataRow studentRow, DataView courseDV)
    {
    DateTime courseStatusDat eCentralTime = DateTime.MinVal ue;
    DateTime coursestatusdat e = DateTime.MinVal ue;

    if (courseRow["coursestatusda te"].ToString() != string.Empty)
    {
    coursestatusdat e = DateTime.Parse( courseRow["coursestatusda te"].ToString());
    //TODO: Convert coursestatusdat e to CST
    courseStatusDat eCentralTime = coursestatusdat e.Add(System.Ti meSpan.FromHour s(-1));
    }

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      Represents an instant in time, typically expressed as a date and time of day.


      I think you need the AddHours method.

      Code:
                  DateTime now = DateTime.Now;
                  DateTime nowMinusOne = now.AddHours(-1);
      
                  Console.WriteLine(now.ToString());
                  Console.WriteLine(nowMinusOne.ToString());
      By the way, I notice you're talking about timezones in there... a handy tip if you're going to do any compares. There's a ToUniversalTime method off a DateTime object that will convert a time to universal time. I don't now if you know about it already, but I discovered it recently so maybe it will save you some trouble in the future :)

      *Edit: Looks like you got got it another way, nice work!

      Comment

      Working...