Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to manipulate a DateTime

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

    How to manipulate a DateTime

    Hello,
    This seems to be a very simple question but cannot find an answer. Please help.
    I have defined a DateTime. How do I replace the date with a differfent date from another DateTime? I want to keep the time as it is.
    Many thanks,
    Morris

    #2
    Hello morrnel,

    You would have to make a new DateTime object to do that. You can use the time from the original DateTime and then specify a date while creating a new DateTime. To learn about DateTime objects I would suggest using external C# learning resources. Here are a few public resources:

    Here is a detailed tutorial on C# DateTime class and how to work with dates and times using C#.


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


    Code:
    DateTime date1 = new DateTime(Year, Month, Day, Hour, Minute, Seconds);
    JesseNinjaTrader Customer Service

    Comment


      #3
      Expanding on Jesse's answer,

      // assume these values
      DateTime d1 = new DateTime(2021, 1, 1, 23, 23, 23);
      DateTime d2 = new DateTime(2022, 2, 2, 12, 12, 12);

      // creates new datetime with values: 2021, 1, 1, 12, 12, 12
      DateTime d3 = new DateTime(d1.Year, d1.Month, d1.Day, d2.Hour, d2.Minute, d2.Seconds);


      Make sense?

      That is,
      d3 is same date as d1 but using the time from d2.

      EDIT:
      Or, to 'replace' the original, reassign everything back to d1,
      and you don't need a new d3 variable,

      d1 = new DateTime(d1.Year, d1.Month, d1.Day, d2.Hour, d2.Minute, d2.Seconds);

      EDIT-2:
      or like you say, you want to change the date,
      but keep the time as it is,

      d1 = new DateTime(d2.Year, d2.Month, d2.Day, d1.Hour, d1.Minute, d1.Seconds);
      ​​
      Last edited by bltdavid; 02-01-2023, 10:04 AM.

      Comment


        #4
        Explained poorly. Try again.

        Let's say I have two DateTime values, a and b. I want to make the equivelent of DateTime c = ToDay(a) + ToTime(b) . Tried this and does not work.

        Comment


          #5
          Hello morrnel,

          What bltdavid posted is valid and is the correct way to combine two DateTime objects. Please see the red/blue highlights that were added to that post which helps to see how each datetime is used.

          To combine DateTime objects you need to make a new DateTime object and use its constructor to be able to build the new date/time from the other DateTimes values. If that is not clear you can also use any external C# educational reference that works with DateTime objects, NinjaScript is C# programming language.

          You can also copy/paste the samples from post 3 into a script to try those samples out. I would suggest using a Print to see the datetime as a string in the output window. https://ninjatrader.com/support/help...lightsub=print
          Code:
          // assume these values
          DateTime d1 = new DateTime(2021, 1, 1, 23, 23, 23);
          DateTime d2 = new DateTime(2022, 2, 2, 12, 12, 12);
          
          // creates new datetime with values: 2021, 1, 1, 12, 12, 12
          DateTime d3 = new DateTime(d1.Year, d1.Month, d1.Day, d2.Hour, d2.Minute, d2.Second);​
          
          Print(d1);
          Print(d2);
          Print(d3);
          Last edited by NinjaTrader_Jesse; 02-03-2023, 08:38 AM.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Do you know the example you copied actually works?

            Comment


              #7
              Hello morrnel,

              Yes that is an accurate description of how to create a new DateTime, if you are having a problem you would need to provide more detail about what problem you are having.

              The previous post from bltdavid has formatting which can cause compile errors if you just tried to copy/paste it. I had highlighted a part of what was in that post so that may still contain errors. The intention of that really wasn't for you to copy/paste and directly use but instead to explain the concept in a way that you can understand.

              If the description of how to make a new DateTime is still not clear from the previously given information and samples I would suggest to not use the NinjaTrader forums for this question but instead look at external C# DateTime tutorials. DateTime objects are not specific to NinjaScript so we have very limited information in our documentation/samples on that topic. DateTimes are part of the C# language so you will find much more helpful information or full samples by looking for public C# learning resources.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Rather than continue, I asked AI for advice.

                "In c# i have two dattime fields, a and b.I want to take the date from a and add the time from b"

                Response:​

                DateTime a = ...;
                DateTime b = ...;
                DateTime result = a.Date + b.TimeOfDay;


                And WOW, it worked!

                Since the release of chatGPT, I have used it to answer so many quesitons with awesome results. This is the first time asking it a NinjaTrader question.




                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by algospoke, Today, 06:40 PM
                0 responses
                2 views
                0 likes
                Last Post algospoke  
                Started by maybeimnotrader, Today, 05:46 PM
                0 responses
                6 views
                0 likes
                Last Post maybeimnotrader  
                Started by quantismo, Today, 05:13 PM
                0 responses
                6 views
                0 likes
                Last Post quantismo  
                Started by AttiM, 02-14-2024, 05:20 PM
                8 responses
                168 views
                0 likes
                Last Post jeronymite  
                Started by cre8able, Today, 04:22 PM
                0 responses
                8 views
                0 likes
                Last Post cre8able  
                Working...
                X