Multiply TimeSpan in .NET



  • 6 Answer(s)

    One can use the code simply,

    TimeSpan duration = TimeSpan.FromMinutes(1);
    duration = TimeSpan.FromTicks(duration.Ticks * 12);
    Console.WriteLine(duration);
    
    Answered on December 25, 2018.
    Add Comment

    Try this code,

    namespace Utility
    {
        public static class TimeSpanExtension
        {
            /// <summary>
            /// Multiplies a timespan by an integer value
            /// </summary>
            public static TimeSpan Multiply(this TimeSpan multiplicand, int multiplier)
            {
            return TimeSpan.FromTicks(multiplicand.Ticks * multiplier);
            }
     
            /// <summary>
            /// Multiplies a timespan by a double value
            /// </summary>
            public static TimeSpan Multiply(this TimeSpan multiplicand, double multiplier)
            {
            return TimeSpan.FromTicks((long)(multiplicand.Ticks * multiplier));
            }
        }
    }
    

    From example usage,

    using Utility;
    private static void Example()
    {
        TimeSpan t = TimeSpan.FromSeconds(30).Multiply(5);
    }
    

    At finally, t which in the code will end up as 150 seconds.

    Answered on December 25, 2018.
    Add Comment

    Use this code, for get the solution. Write this code yourself, because the TimeSpan structure does not provide an overload for the * operator.

    var result = TimeSpan.FromTicks(duration.Ticks * 5);
    
    
    Answered on December 25, 2018.
    Add Comment
    TimeSpan represents a length of time. We can create or manipulate TimeSpan instances in a C# program. The TimeSpan type provides many helper properties and methods.

    A struct. TimeSpan is implemented as a struct type. We use its many constructors to specify a new TimeSpan. We can add TimeSpans, or subtract them to get elapsed times.
    using System;
    
    class Program
    {
        static void Main()
        {
            // Use TimeSpan constructor to specify:
            // ... Days, hours, minutes, seconds, milliseconds.
            // ... The TimeSpan returned has those values.
            TimeSpan span = new TimeSpan(1, 2, 0, 30, 0);
            Console.WriteLine(span);
        }
    }
    Answered on January 15, 2019.
    Add Comment
    using System;
    
    class Program
    {
        static void Main()
        {
            // Get time spans from a specific double unit of time.
            // ... These allow easier manipulation of the time.
            TimeSpan span1 = TimeSpan.FromDays(1);
            TimeSpan span2 = TimeSpan.FromHours(1);
            TimeSpan span3 = TimeSpan.FromMinutes(1);
            TimeSpan span4 = TimeSpan.FromSeconds(1);
            TimeSpan span5 = TimeSpan.FromMilliseconds(1);
    
            Console.WriteLine(span1);
            Console.WriteLine(span2);
            Console.WriteLine(span3);
            Console.WriteLine(span4);
            Console.WriteLine(span5);
        }
    }
    
    Output
    
    1.00:00:00
    01:00:00
    00:01:00
    00:00:01
    00:00:00.0010000
    using System;
    
    class Program
    {
        static void Main()
        {
            // Get time spans from a specific double unit of time.
            // ... These allow easier manipulation of the time.
            TimeSpan span1 = TimeSpan.FromDays(1);
            TimeSpan span2 = TimeSpan.FromHours(1);
            TimeSpan span3 = TimeSpan.FromMinutes(1);
            TimeSpan span4 = TimeSpan.FromSeconds(1);
            TimeSpan span5 = TimeSpan.FromMilliseconds(1);
    
            Console.WriteLine(span1);
            Console.WriteLine(span2);
            Console.WriteLine(span3);
            Console.WriteLine(span4);
            Console.WriteLine(span5);
        }
    }
    
    Output
    
    1.00:00:00
    01:00:00
    00:01:00
    00:00:01
    00:00:00.0010000
    
    
    Answered on January 15, 2019.
    Add Comment
    TimeSpan day = TimeSpan.FromDays(1);
    TimeSpan week = TimeSpan.FromTicks(day.Ticks * 7);
    Answered on January 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.