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:



    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 DawnTreader, 05-08-2024, 05:58 PM
                52 responses
                308 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by grazz, 07-28-2017, 06:11 PM
                27 responses
                6,848 views
                0 likes
                Last Post NinjaTrader_ChristopherJ  
                Started by frslvr, Today, 11:36 AM
                4 responses
                14 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by tkaboris, 07-15-2024, 01:42 PM
                13 responses
                25 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by richard_w, Today, 12:33 PM
                1 response
                7 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Working...
                X