Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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
    BertrandNinjaTrader Customer Service

    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();
        BertrandNinjaTrader Customer Service

        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 Shansen, 08-30-2019, 10:18 PM
                  25 responses
                  949 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by JonesJoker, 04-22-2024, 12:23 PM
                  8 responses
                  41 views
                  0 likes
                  Last Post JonesJoker  
                  Started by timko, Today, 06:45 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post timko
                  by timko
                   
                  Started by Waxavi, 04-19-2024, 02:10 AM
                  2 responses
                  39 views
                  0 likes
                  Last Post poeds
                  by poeds
                   
                  Started by chbruno, Yesterday, 04:10 PM
                  1 response
                  44 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Working...
                  X