Skip to content

JonasSchubert/TimeSpan.Xt

Repository files navigation

timext

TimeSpan.Xt

First of all many thanks to Kizitonwose for the original idea and already awesome library!

This .NET Standard 2.0 library shall help to reduce code like

dotnet add package TimeSpan.Xt
var dayInMillis = 24 * 60 * 60 * 1000;				// Represent a day in milliseconds
var inFiveDays = DateTime.Now + TimeSpan.FromDays(5);
var oneWeekAgo = DateTime.Now - TimeSpan.FromDays(7);

How to use

using TimeSpan.Xt;

var fiveCenturies = 5.Centuries();				// uses precise calculation: 		1 Century == 36500 days
var fiveCenturiesAccumulated = 5.Centuries(true);		// uses accumulated calculation: 	1 Century == 36525 days
var tenDecades = 10.0.Decades();				// uses precise calculation: 		1 Decade == 3650 days
var tenDecadesAccumulated = 10.0.Decades(true);			// uses accumulated calculation: 	1 Decade == 3652.5 days
var twoYears = 2.Years();					// uses precise calculation: 		1 Year == 365 days
var twoYearsAccumulated = 2.Years(true);		        // uses accumulated calculation: 	1 Year == 365.25 days
var oneWeek = 1.Weeks();
var threeDays = 3.Days();
var elevenHours = 11.Hours();
var sixMinutes = 6.Minutes();
var fiftySeconds = 50.Seconds();
var hundredMilliSeconds = 100.Milliseconds();

var oneDayInMillis = 1.Days().InMilliseconds();    		// Converts one day into milliseconds and returns the double value => 86400000
var twoWeeksInHours = 2.Weeks().InHours(); 		    	// Converts two weeks into hours and returns the double value => 336
var thirtyFiveDaysInWeeks = 35.Days().InWeeks(); 		// Converts 35 days into weeks and returns the double value => 5

var addition = 1.Days() + 12.Hours() + 30.Minutes();
var difference = 34.Minutes() - 240.Seconds();
var multiplication = 2.Weeks() * 2;
var division = 2.Minutes() / 2;

var inFiveDays = DateTime.Now + 5.Days();
var yesterday = DateTime.Now - 24.Hours();
var oneWeekAgo = DateTime.Now - 1.Weeks();

Since TimeSpan.Xt is directly interchangable with TimeSpan, it can be used in all places where you would normally use TimeSpan, e.g.

Task.Delay(5.Seconds())

Contributors

JonasSchubert DenisBiondic
Jonas Schubert Denis Biondic

License

TimeSpan.Xt is distributed under the MIT license. See LICENSE for details.

MIT License

Copyright (c) 2018-2024 JonasSchubert

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.