none
TimeSpan for years, months and days

    Question

  • This example bellow is only for days, hours, minutes, I would like the same one for years, months and days:

    global variablal:
    TimeSpan abc;

    abc = ExpiredTime - TimeNow;
    string countDown = string.Format("{0} days, {1} hours, {2} minutes", abc.Days, abc.Hours, abc.Minutes);

    I am a rookie at C#, so please don`t be mad if my questions goona sound stupid...
    Wednesday, June 10, 2009 7:22 PM

Answers

All replies

  • I believe that the best you can do is Days. You would have to convert to months and years yourself.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, June 10, 2009 7:30 PM
  • I got something:
    DateTime Birth = new DateTime(1954, 7, 30);
            DateTime Today = DateTime.Now;
    
            TimeSpan Span = Today - Birth;
    
            DateTime Age = DateTime.MinValue + Span;
    
            // note: MinValue is 1/1/1 so we have to subtract...
            int Years = Age.Year - 1;
            int Months = Age.Month - 1;
            int Days = Age.Day - 1;
            Response.Write(Years.ToString() + " Years, " + Months.ToString() + " Months, " + Days.ToString() + " Days<br />");
    But where to get "Response". It is not available!

    I am a rookie at C#, so please don`t be mad if my questions goona sound stupid...
    • Proposed as answer by viserto Wednesday, November 02, 2011 9:32 AM
    Wednesday, June 10, 2009 7:34 PM
  • Are you creating a Web page? Response is for writing out to Web pages.

    If you just want to write out your code to the Debug window, use Debug.WriteLine(...)
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, June 10, 2009 7:43 PM
  • I did it. It works.
    But now I would like to add an if statement.

    if(database column name "EndTime" is not NULL)
    {
        the code above!
    }

    I am a rookie at C#, so please don`t be mad if my questions goona sound stupid...
    Wednesday, June 10, 2009 7:52 PM
  • Mitja,

    Use the if statement from my response at your other thread:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/5ff28948-1dee-41fc-9b38-1477a15ecd3f

    Read all the information about DBNull and how to detect if it has been returned from the database or not.
    David Morton - http://blog.davemorton.net/ - @davidmmorton
    • Marked as answer by Harry Zhu Wednesday, June 17, 2009 10:25 AM
    Wednesday, June 10, 2009 7:56 PM
    Moderator
  • I got an error on inserting into Database. What is wrong with conversation?
                            SqlCommand cmd4 = new SqlCommand("UPDATE Components SET Components.StartTime = @startTime " +
                                                            "WHERE Components.IDComponent = '" + IDKomponente + "'");
                            SqlParameter param4 = new SqlParameter();
                            param4.ParameterName = "@startTime";
                            if (string.IsNullOrEmpty(dateTimePickerStart.Text))
                            {
                                param4.Value = System.DBNull.Value;
                            }
                            else
                                param4.Value = Convert.ToDateTime(dateTimePickerStart.Text).ToString("dd.MM.yyyy");
                            cmd4.Parameters.Add(param4);
                            cmd4.Connection = povezava;
                            povezava.Open();
                            cmd4.ExecuteNonQuery();
                            povezava.Close();


    I am a rookie at C#, so please don`t be mad if my questions goona sound stupid...
    Wednesday, June 10, 2009 9:48 PM
  • It's with the line within the else statement. 

    You shouldn't pass .ToString() when sending it to the database.  You're sending the data in as a DateTime right?  Just do:

    param4.Value = Convert.ToDateTime(dateTimePickerStart.Text);

    Or better yet:

    param4.Value = dateTimePickerStart.Value;


    David Morton - http://blog.davemorton.net/ - @davidmmorton
    • Marked as answer by Harry Zhu Wednesday, June 17, 2009 10:25 AM
    Wednesday, June 10, 2009 10:14 PM
    Moderator
  • thx David, I have figurated that out by my selft yest. Thx anyway :) 
    You are the man!


    I am a rookie at C#, so please don`t be mad if my questions goona sound stupid...
    Thursday, June 11, 2009 6:56 AM