C# में TimeSpan.Subtract() विधि का उपयोग एक नई TimeSpan ऑब्जेक्ट को वापस करने के लिए किया जाता है जिसका मान निर्दिष्ट TimeSpan ऑब्जेक्ट और इस उदाहरण के बीच का अंतर है।
सिंटैक्स
वाक्य रचना इस प्रकार है -
public TimeSpan Subtract (TimeSpan span);
ऊपर, पैरामीटर अवधि घटाया जाने वाला समय अंतराल है।
उदाहरण
आइए अब एक उदाहरण देखें -
using System; public class Demo { public static void Main(){ TimeSpan span1 = TimeSpan.FromTicks(1); TimeSpan span2 = new TimeSpan(1); TimeSpan span3 = TimeSpan.FromHours(1); TimeSpan span4 = TimeSpan.FromMilliseconds(1); TimeSpan span5 = TimeSpan.FromMinutes(1); TimeSpan span6 = TimeSpan.FromMinutes(1); Console.WriteLine("TimeSpan1 = "+span1); Console.WriteLine("TimeSpan2 = "+span2); Console.WriteLine("TimeSpan3 = "+span3); Console.WriteLine("TimeSpan4 = "+span4); Console.WriteLine("TimeSpan5 = "+span5); Console.WriteLine("Result (Comparison of span1 and span2) = "+TimeSpan.Compare(span1, span2)); Console.WriteLine("Result (Comparison of span2 and span3) = "+TimeSpan.Compare(span2, span3)); Console.WriteLine("Result (Comparison of span1 and span3) = "+TimeSpan.Compare(span1, span3)); Console.WriteLine("Result (Comparison of span3 and span4) = "+TimeSpan.Compare(span3, span4)); Console.WriteLine("Result (Comparison of span4 and span5) = "+TimeSpan.Compare(span4, span5)); TimeSpan res1 = span1.Subtract(span2); TimeSpan res2 = span2.Subtract(span2); Console.WriteLine("Result = "+res1); Console.WriteLine("Result = "+res2); } }
आउटपुट
यह निम्नलिखित आउटपुट देगा -
TimeSpan1 = 00:00:00.0000001 TimeSpan2 = 00:00:00.0000001 TimeSpan3 = 01:00:00 TimeSpan4 = 00:00:00.0010000 TimeSpan5 = 00:01:00 Result (Comparison of span1 and span2) = 0 Result (Comparison of span2 and span3) = -1 Result (Comparison of span1 and span3) = -1 Result (Comparison of span3 and span4) = 1 Result (Comparison of span4 and span5) = -1 Result = 00:00:00 Result = 00:00:00
उदाहरण
आइए अब एक और उदाहरण देखें -
using System; public class Demo { public static void Main(){ TimeSpan span1 = TimeSpan.FromTicks(1500); TimeSpan span2 = new TimeSpan(2, 40, 55); TimeSpan span3 = TimeSpan.FromHours(5); TimeSpan span4 = TimeSpan.FromMilliseconds(1000); TimeSpan span5 = TimeSpan.FromMinutes(50); TimeSpan span6 = TimeSpan.FromMinutes(50); Console.WriteLine("TimeSpan1 = "+span1); Console.WriteLine("TimeSpan2 = "+span2); Console.WriteLine("TimeSpan3 = "+span3); Console.WriteLine("TimeSpan4 = "+span4); Console.WriteLine("TimeSpan5 = "+span5); Console.WriteLine("Result (Comparison of span1 and span2) = "+TimeSpan.Compare(span1, span2)); Console.WriteLine("Result (Comparison of span2 and span3) = "+TimeSpan.Compare(span2, span3)); Console.WriteLine("Result (Comparison of span1 and span3) = "+TimeSpan.Compare(span1, span3)); Console.WriteLine("Result (Comparison of span3 and span4) = "+TimeSpan.Compare(span3, span4)); Console.WriteLine("Result (Comparison of span4 and span5) = "+TimeSpan.Compare(span4, span5)); TimeSpan res1 = span1.Subtract(span2); TimeSpan res2 = span2.Subtract(span2); Console.WriteLine("Result = "+res1); Console.WriteLine("Result = "+res2); } }
आउटपुट
यह निम्नलिखित आउटपुट देगा -
TimeSpan1 = 00:00:00.0001500 TimeSpan2 = 02:40:55 TimeSpan3 = 05:00:00 TimeSpan4 = 00:00:01 TimeSpan5 = 00:50:00 Result (Comparison of span1 and span2) = -1 Result (Comparison of span2 and span3) = -1 Result (Comparison of span1 and span3) = -1 Result (Comparison of span3 and span4) = 1 Result (Comparison of span4 and span5) = -1 Result = -02:40:54.9998500 Result = 00:00:00