Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple format question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Simple format question

    Code:
    DateTime date2 = DateTime.Now;
    	System.TimeSpan diff =( date2 - tradetimex);


    There must be a simple way to put the above timespan into a string("t")fomat - but I have tried for 2 hours and I just can't do it!!

    My workaround currently involves separating diff into it's component hours,mins and seconds and then re stringing them all together which seems
    very long winded.

    I know this is unsupported by NT but perhaps someone from the community could assist/show me?

    #2
    Hi Mindset, did you already check the ideas provided in this tip? - http://www.ninjatrader-support2.com/...ead.php?t=3823

    Comment


      #3
      Issues

      Bertrand
      Thanks I did look there.

      string newtime = diff; gives an implicit conversion error
      string newtime = diff.Hours gives me a single component of the timespan .
      What I require is the whole timespan in a short time format

      eg 1:03:17

      If I make newtime an integer it compiles but the string won't output.

      I looked at msdn and found there is a ToString and Parse overrides but I couldn't get them to work. Only been coding for 4 months.
      Seems so simple.

      Comment


        #4
        Have you tried - string newtime = diff.ToString();

        Comment


          #5
          getting somewhere

          Code:
          DateTime date2 = DateTime.Now;
          System.TimeSpan diff =( date2 - tradetimex);
          TimeSpan lapse = diff.Duration();
          string lapsedTime = lapse.ToString();
          lapsedTime = lapsedTime.Substring(0,lapsedTime.LastIndexOf("."));
          Right this gives me the time but I have 4 digits in my seconds area?.
          Last edited by Mindset; 04-14-2009, 01:38 PM.

          Comment


            #6
            You will need to format it yourself. Using ToString() already brings it to a string. It is just string manipulation after that.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              can't override the ToString()

              that's what I thought Josh!

              If I try lapsedTime("t") or lapsedTime("hh:mm:ss") neither works due to an invalid argument and that I can't convert from a string to SystemIFormatProvider?
              Last edited by Mindset; 04-14-2009, 01:46 PM. Reason: editing

              Comment


                #8
                Mindset,

                You likely can't format it like that. Your string consists of 00:00:00 or similar. These are not numbers already. You will need to cut/splice/etc. to get it the way you want it.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Outcome

                  In the end it would appear you do as much mucking about with the string as my original code so I shall stick with that.
                  I still don't see why the seconds element comes out as 4 digits but life is too short to worry about it and you never know maybe some bright spark will come up with a more elegant solution.
                  In the meantime I post my clunky solution below.

                  Code:
                  DateTime date2 = DateTime.Now;
                  System.TimeSpan diff = date2.Subtract(tradetimex);
                  StringBuilder dBox = new StringBuilder();
                  if(diff.Hours > 0)
                  {
                  dBox.Append(diff.Hours+ "h:");// ie if zero don't show
                  }
                  
                  if(diff.Minutes <10)
                  {
                  dBox.Append("0");// adds "0" to mins < 10
                  }				
                  			
                  dBoxLine2.Append(diff.Minutes+"m:");
                  			
                  if(diff.Seconds <10)
                  {
                  dBox.Append("0");// as above
                  }

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by sjsj2732, Yesterday, 04:31 AM
                  0 responses
                  35 views
                  0 likes
                  Last Post sjsj2732  
                  Started by NullPointStrategies, 03-13-2026, 05:17 AM
                  0 responses
                  286 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  286 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  133 views
                  1 like
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  92 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Working...
                  X