Loading repository data…
Loading repository data…
JonasSchubert / repository
A curated collection of useful C# snippets that you can understand in 30 seconds or less.
A curated collection of useful C# snippets that you can understand in 30 seconds or less.
Note: This project is inspired by 30 Seconds of Code, but there is no affiliation with that project.
dayOfYearformatDurationgetColonTimeFromDategetDaysDiffBetweenDatesgetMeridiumSuffixOfIntegerisAfterDateisBeforeDateisSameDatemaxDateminDatetomorrowallEqualbifurcatebubbleSortchunkcompactcountBycountOccurencesdeepFlattendifferencedifferenceSelectdifferenceWheredropdropRightdropRightWhiledropWhileeveryNthfilterNonUniquefilterNonUniqueByfindLastfindLastIndexflattenforEachRighthasDuplicatesindexOfAllapproximatelyEqualaverageaverageBybinomialCoefficientdegToRadsdigitizedistancefactorialfibonaccigcdgeometricProgressioninRangeisDivisibleByisEvenisPrimeisOddlcmluhnCheckmaxmedianminprimesradToDegrandomIntArrayInRangerandomIntegerInRangebyteSizecountVowelscsvToArraycsvToJsonendsWithRegexfromCamelCaseisAnagramOfisLowerisPalindromeisUppermaskpadremoveNonAsciireversesplitLinesstartsWithRegexstripHtmlTagstoCamelCasetoKebabCasetoSnakeCasetoTitleCasetruncatewordsReturns the day of the current year.
Already integrated here.
using System;
namespace JonasSchubert.Snippets.Date
{
public static partial class Date
{
public static int DayOfYear()
{
return DateTime.UtcNow.DayOfYear;
}
}
}
JonasSchubert.Snippets.Date.DayOfYear() # 12/31/2016: day 366 of 2016 (Leap Year)
Returns the human readable format of the given number of milliseconds.
namespace JonasSchubert.Snippets.Date
{
public static partial class Date
{
public static string FormatDuration(ulong milliseconds)
{
var dictionary = new Dictionary<string, Tuple<ulong, uint>>
{
{ "week", new Tuple<ulong, uint>(7* 24 * 60 * 60 * 1000, int.MaxValue) },
{ "day", new Tuple<ulong, uint>(24 * 60 * 60 * 1000, 7) },
{ "hour", new Tuple<ulong, uint>(60 * 60 * 1000, 24) },
{ "minute", new Tuple<ulong, uint>(60 * 1000, 60) },
{ "second", new Tuple<ulong, uint>(1000, 60) },
{ "millisecond", new Tuple<ulong, uint>(1, 1000) }
};
var stringArray = dictionary
.Select(item =>
((milliseconds / item.Value.Item1) % item.Value.Item2) > 0
? $"{((milliseconds / item.Value.Item1) % item.Value.Item2)} {item.Key}{(((milliseconds / item.Value.Item1) % item.Value.Item2) > 1 ? "s" : string.Empty)}"
: string.Empty)
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
return stringArray.Length > 0
? string.Join(", ", stringArray)
: "0 millisecond";
}
}
}
var actual = Date.FormatDuration(34325055574ul); # "56 weeks, 5 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds"
Returns a string of the form HH:MM:SS from a DateTime or TimeSpan object.
namespace JonasSchubert.Snippets.Date
{
public static partial class Date
{
public static string GetColonTimeFromDate(this DateTime dateTime)
{
return $"{dateTime.Hour.ToString("D2")}:{dateTime.Minute.ToString("D2")}:{dateTime.Second.ToString("D2")}";
}
public static string GetColonTimeFromDate(this TimeSpan timeSpan)
{
return $"{timeSpan.Hours.ToString("D2")}:{timeSpan.Minutes.ToString("D2")}:{timeSpan.Seconds.ToString("D2")}";
}
}
}
new DateTime(2018, 11, 22, 17, 53, 23).GetColonTimeFromDate() # 17:53:23
new DateTime(1990, 1, 2, 3, 41, 5).GetColonTimeFromDate() # 03:41:05
new TimeSpan(1, 33, 7).GetColonTimeFromDate() # 01:33:07
Returns the difference (in days) between two dates.
using System;
namespace JonasSchubert.Snippets.Date
{
public static partial class Date
{
public static double GetDaysDiffBetweenDates(DateTime dateTime1, DateTime dateTime2)
{
if (dateTime1.Kind != dateTime2.Kind)
{
throw new ArgumentException($"The DateTime values have to be in the same timezone! {nameof(dateTime1)} uses {dateTime1.Kind}, while {nameof(dateTime2)} uses {dateTime2.Kind}!");
}
return (dateTime1 - dateTime2).TotalDays;
}
}
}
Date.GetDaysDiffBetweenDates(new DateTime(2018, 11, 22), new DateTime(2018, 11, 14)); # 8.0
Converts an integer to a suffixed string, adding am or pm based on its value.
using System;
namespace JonasSchubert.Snippets.Date
{
public static partial class Date
{
public static string GetMeridiemSuffixOfInteger(this int value)
{
if (value < 0 || value > 24)
{
throw new ArgumentOutOfRangeException($"Invalid value {value} in method {nameof(GetMeridie
initial