Last active
March 13, 2019 19:54
-
-
Save Pavelyev/9d3025bda6776efd6deab4a33a178a73 to your computer and use it in GitHub Desktop.
DateExt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class DateExtensions | |
{ | |
public static string ToText(this DateTime date) | |
{ | |
var hour = (DateField)date.Hour; | |
bool zy = false, zm = false, zd = false; | |
if ((hour & DateField.Year) > 0) | |
zy = true; | |
if ((hour & DateField.Month) > 0) | |
zm = true; | |
if ((hour & DateField.Day) > 0) | |
zd = true; | |
if (zd & zm) | |
return $"год {date.Year} "; | |
if (zd) | |
return $"год {date.Year} месяц {date.Month}"; | |
if (zy) | |
return $"год не указан"; | |
return date.ToShortDateString(); | |
} | |
public static string ToText(this DateTime? date) => date.HasValue ? ToText(date.Value) : ""; | |
public static DateTime Build(int year, int month, int day) | |
{ | |
DateField mask = 0; | |
if (year == 0) | |
{ | |
year = 1; | |
mask = mask | DateField.Year; | |
} | |
if (month == 0) | |
{ | |
month = 1; | |
mask = mask | DateField.Month; | |
} | |
if (day == 0) | |
{ | |
day = 1; | |
mask = mask | DateField.Day; | |
} | |
return new DateTime(year, month, day, mask, 0, 0); | |
} | |
[Flags] | |
enum DateField | |
{ | |
Year = 1, | |
Month = 2, | |
Day = 4 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment