Skip to content

Commit

Permalink
feat: added date time extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
aochmann committed Feb 15, 2021
1 parent 3019c6c commit c5457a9
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Source/Cogworks.Essentials/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Globalization;

namespace Cogworks.Essentials.Extensions
{
public static class DateTimeExtension
{
public static int GetUnixTimeStamp(this DateTime date)
=> (int)date.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;

public static string ToShortDate(this DateTime date)
=> date.ToString("dd MMM yyyy");

public static string GetLongMonthName(this DateTime date, CultureInfo culture = null,
string defaultCultureCode = null)
{
if (!defaultCultureCode.HasValue())
{
defaultCultureCode = CultureInfo.CurrentCulture.TextInfo.CultureName;
}

if (!culture.HasValue())
{
culture = CultureInfo.GetCultureInfo(defaultCultureCode);
}

return date.ToString("MMMM", culture);
}

public static DateTime ToDateTime(this long ticks)
=> new DateTime(ticks);

public static bool HasValue(this DateTime date)
=> date != default;
}
}

0 comments on commit c5457a9

Please sign in to comment.