7 Replies Latest reply on Mar 18, 2016 9:32 AM by gregor

    AFTime.Subtraction

    Argel

      I saw this post AFTime.Subtraction Operator (AFTime, AFTime) and I don't know how to use it.

       

      Can someone help me please?

       

      And I've read that the result is a timespan. How can I get the number of minutes on the resulting timespan.

       

      Please share me some piece of code.

       

      Thanks!

        • Re: AFTime.Subtraction
          Nocodes79

          HI Argel,

           

          TimeSpan has a TotalMinutes property:

           

          double interval = (endTime - startTime).TotalMinutes;
          

           

          where endTime and startTime are AFTime

          1 of 1 people found this helpful
          • Re: AFTime.Subtraction
            Roger Palmen

            You could use the AFTimeSpan.ToTimeSpan Method and then use the TimeSpan.Minutes Property (System) property.

              • Re: AFTime.Subtraction
                Argel

                Hi Roger,

                 

                Can you please give me a simple sample code. I don't really know how to use these. Sorry.

                  • Re: AFTime.Subtraction
                    gregor

                    Hello Argel,

                     

                    The implementation you referred to allows to calculate a TimeSpan by substracting 2 AFTime objects.

                     

                            static void Main(string[] args)
                            {
                                string timeStampFormat = "dd-MMM-yyyy hh:mm:ss.fff";
                                AFTime endTime = new AFTime("*");
                                AFTime startTime = new AFTime("*-8h");
                                TimeSpan timeSpan = endTime - startTime;
                                string startTimeString = startTime.ToString(timeStampFormat);
                                string endTimeString = endTime.ToString(timeStampFormat);
                                Console.WriteLine("{0} - {1} = {2}", endTimeString, startTimeString, timeSpan.TotalMinutes);
                                Console.ReadKey();
                            }
                    

                     

                    The interesting thing is that with the abovethe resulting TimeSpan sometimes has fractions because the way startTime and endTime are being defined is not accurate. If you want to use the sub second portion, fine. Otherwise here's a way to be more precise with the definitions:

                     

                            static void Main(string[] args)
                            {
                                string timeStampFormat = "dd-MMM-yyyy hh:mm:ss.fff";
                                AFTime endTime = AFTime.NowInWholeSeconds;
                                AFTime startTime = endTime.LocalTime.AddHours(-8);
                                TimeSpan timeSpan = endTime - startTime;
                                string startTimeString = startTime.ToString(timeStampFormat);
                                string endTimeString = endTime.ToString(timeStampFormat);
                                Console.WriteLine("{0} - {1} = {2}", endTimeString, startTimeString, timeSpan.TotalMinutes);
                                Console.ReadKey();
                            }
                    
                    1 of 1 people found this helpful