using System.Globalization; namespace Kit.Helpers { public class WeekInfo { public int NumberOfYear { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } } public static partial class SystemDateTimeExtensionMethods { public static string ToStringDateRus(this DateTime dt) { return dt.ToString("dd.MM.yyyy"); } public static string ToStringDateTimeRus(this DateTime dt) { return dt.ToString("dd.MM.yyyy hh:mm"); } public static string ToStringTimeRus(this DateTime dt) { return dt.ToString("hh:mm:ss"); } public static string ToStringDateRus(this DateTime? dt) { if (dt == null) return String.Empty; return dt.ToStringDateRus(); } public static string ToStringDateTimeRus(this DateTime? dt) { if (dt == null) return String.Empty; return dt.Value.ToStringDateTimeRus(); } public static string ToStringTimeRus(this DateTime? dt) { if (dt == null) return String.Empty; return dt.Value.ToStringTimeRus(); } private static CultureInfo cultureRu = new CultureInfo("ru-Ru"); public static string GetStringMonthYear(this DateTime date) { return date.ToString("Y"); } //public static DateTime Normolize(this DateTime currentValue) //{ // return currentValue.Normolize(DateTime.MinValue, DateTime.Now); //} public static DateTime GetClearTime(this DateTime date) { return new DateTime(date.Year, date.Month, date.Day); } private static readonly long DatetimeMinTimeTicks = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks; public static long ToJavaScriptMilliseconds(this DateTime dt) { return (long)((dt.ToUniversalTime().Ticks - DatetimeMinTimeTicks) / 10000); } public static DateTime GetUtcFromDateTime(this DateTime dt, string timezene) { TimeZoneInfo tzf; try { tzf = TimeZoneInfo.FindSystemTimeZoneById(timezene); } catch { tzf = TimeZoneInfo.FindSystemTimeZoneById("Russian Standard Time"); } return TimeZoneInfo.ConvertTimeToUtc(dt, tzf); } public static DateTime GetDateTimeFromUtc(this DateTime dt, string timezene) { TimeZoneInfo tzf; try { tzf = TimeZoneInfo.FindSystemTimeZoneById(timezene); } catch { tzf = TimeZoneInfo.FindSystemTimeZoneById("Russian Standard Time"); } return TimeZoneInfo.ConvertTimeFromUtc(dt, tzf); } public static string ToStringOrEmpty(this DateTime? dt, string format) { if (dt == null) return String.Empty; return dt.Value.ToString(format); } public static string ToStringOrEmpty(this DateTime? dt) { if (dt == null) return String.Empty; return dt.Value.ToString("dd.MM.yyyy"); } public static string ToShortDateStringRu(this DateTime value) { return value.ToString("dd.MM.yyyy"); } public static string ToShortDateStringUniversal(this DateTime value) { return value.ToString("yyyy.MM.dd"); } public static string ToStringDMYHMS(this DateTime value) { return value.ToString("dd.MM.yyyy hh:mm:ss"); } public static string ToShortDateStringRu(this DateTime? value) { if (value == null) return String.Empty; return value.Value.ToShortDateStringRu(); } public static string ToShortDateStringUniversal(this DateTime? value) { if (value == null) return String.Empty; return value.Value.ToShortDateStringUniversal(); } public static string ToStringDMYHMS(this DateTime? value) { if (value == null) return String.Empty; return value.Value.ToStringDMYHMS(); } public static WeekInfo GetInfoWeek(this DateTime? value) { if (!value.HasValue) return null; return value.Value.GetInfoWeek(); } public static WeekInfo GetInfoWeek(this DateTime value) { var result = new WeekInfo(); DateTimeFormatInfo dfi = cultureRu.DateTimeFormat; Calendar cal = dfi.Calendar; result.NumberOfYear = cal.GetWeekOfYear(value, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday); var dtStart = new DateTime(value.Year, 1, 1).AddDays(-(int)(new DateTime(value.Year, 1, 1).DayOfWeek - 1)); result.StartDate = dtStart.AddDays((result.NumberOfYear - 1) * 7); result.EndDate = dtStart.AddDays(result.NumberOfYear * 7); return result; } public static string GetMonthNameRP(this DateTime dt) { return dt.ToString("MMMMMMMMMMM", cultureRu).Replace("т", "та").Replace("тая", "тя").Replace("й", "я").Replace("ь", "я"); } } }