This project has moved. For the latest updates, please go here.

System.TimeSpan Extension Methods

TimeSpan.Ago Method

A TimeSpan extension method that substract the specified TimeStamp to the current DateTime.

Syntax

DateTime Ago(TimeSpan @this)

Parameters

@this
The @this to act on.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_TimeSpan_Ago
     {
         [TestMethod]
         public void Ago()
         {
             // Type
             var @this = new TimeSpan(1, 0, 0, 0);
 
             // Examples
             DateTime value = @this.Ago(); // return yesterday.
 
             // Unit Test
             Assert.IsTrue(DateTime.Now.Subtract(value).Days >= 1);
         }
     }
 }
                

TimeSpan.FromNow Method

A TimeSpan extension method that add the specified TimeStamp to the current DateTime.

Syntax

DateTime FromNow(TimeSpan @this)

Parameters

@this
The @this to act on.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_TimeSpan_FromNow
     {
         [TestMethod]
         public void FromNow()
         {
             // Type
             var @this = new TimeSpan(1, 0, 0, 0);
 
             // Examples
             DateTime value = @this.FromNow(); // return tomorrow.
 
             // Unit Test
             Assert.IsTrue(DateTime.Now.Subtract(value).Days <= -1);
         }
     }
 }
                

TimeSpan.UtcAgo Method

A TimeSpan extension method that substract the specified TimeStamps to the current UTC (Coordinated Universal
Time)

Syntax

DateTime UtcAgo(TimeSpan @this)

Parameters

@this
The @this to act on.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_TimeSpan_UtcAgo
     {
         [TestMethod]
         public void UtcAgo()
         {
             // Type
             var @this = new TimeSpan(1, 0, 0, 0);
 
             // Examples
             DateTime value = @this.UtcAgo(); // return yesterday.
 
             // Unit Test
             Assert.IsTrue(DateTime.UtcNow.Subtract(value).Days >= 1);
         }
     }
 }
                

TimeSpan.UtcFromNow Method

A TimeSpan extension method that add the specified TimeStamps to the current UTC (Coordinated Universal Time)

Syntax

DateTime UtcFromNow(TimeSpan @this)

Parameters

@this
The @this to act on.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_TimeSpan_UtcFromNow
     {
         [TestMethod]
         public void UtcFromNow()
         {
             // Type
             var @this = new TimeSpan(1, 0, 0, 0);
 
             // Examples
             DateTime value = @this.UtcFromNow(); // return yesterday.
 
             // Unit Test
             Assert.IsTrue(DateTime.UtcNow.Subtract(value).Days <= -1);
         }
     }
 }
                

Last edited Nov 23, 2013 at 6:56 PM by Jonathan_Magnan, version 1