Skip to content

Commit

Permalink
Added three new DateTime extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Pouwels committed Oct 14, 2022
1 parent 79e76b4 commit 43d0058
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
44 changes: 44 additions & 0 deletions src/ZimLabs.CoreLib/ZimLabs.CoreLib/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,48 @@ public static string ConvertToFileSize(this FileInfo fileInfo, int divider = 102
return null;
}
#endregion

#region DateTime
/// <summary>
/// Sets the time of the date / time value to the first possible value (00:00:00)
/// </summary>
/// <param name="value">The date / time value</param>
/// <returns>The date / time with the new time</returns>
public static DateTime SetStartOfDay(this DateTime value)
{
return value.SetTime(0, 0, 0);
}

/// <summary>
/// Sets the time of the date / time value to the last possible value (23:59:59)
/// </summary>
/// <param name="value">The date / time value</param>
/// <returns>The date / time value with the new time</returns>
public static DateTime SetEndOfDay(this DateTime value)
{
return value.SetTime(23, 59, 59);
}

/// <summary>
/// Sets the time of the date / time
/// </summary>
/// <param name="value">The date / time value</param>
/// <param name="hour">The hour (0 through 23)</param>
/// <param name="minute">The minutes (0 through 59)</param>
/// <param name="second">The seconds (0 through 59)</param>
/// <returns>The date / time with the new time</returns>
public static DateTime SetTime(this DateTime value, int hour, int minute, int second)
{
if (hour is < 0 or > 23)
hour = 0;

if (minute is < 0 or > 59)
minute = 0;

if (second is < 0 or > 59)
second = 0;

return new DateTime(value.Year, value.Month, value.Day, hour, minute, second);
}
#endregion
}
6 changes: 3 additions & 3 deletions src/ZimLabs.CoreLib/ZimLabs.CoreLib/ZimLabs.CoreLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<PackageTags>helper, extensions</PackageTags>
<PackageReleaseNotes>Added extension to extract a custom attribute</PackageReleaseNotes>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>2.0.1</AssemblyVersion>
<FileVersion>2.0.1</FileVersion>
<Version>2.0.1</Version>
<AssemblyVersion>2.1.0</AssemblyVersion>
<FileVersion>2.1.0</FileVersion>
<Version>2.1.0</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
Expand Down

0 comments on commit 43d0058

Please sign in to comment.