コード例 #1
0
        /// <summary>
        /// Интервал дат без дублирования информации.
        /// Например: с 10 по 25 мая 2000
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="labels"></param>
        /// <returns></returns>
        public static string GetIntervalString(DateTime?from, DateTime?to = null, IList <string> labels = null)
        {
            if (from == null && to == null)
            {
                return("");
            }

            var fromLabel = "с";
            var toLabel   = "по";

            if (labels != null && labels.Count > 0)
            {
                fromLabel = labels[0];
                if (labels.Count > 1)
                {
                    toLabel = labels[1];
                }
            }

            Tuple <string, string> formats;

            if (from.HasValue)
            {
                formats = DateFormatter.GetFormat(from.Value, to); // to may be null
            }
            else // only to.HasValue
            {
                formats = DateFormatter.GetFormat(to.Value, null);
            }

            if (from.HasValue && to.HasValue)
            {
                if (from == to)
                {
                    return(string.Format("{0}", from.Value.ToString(formats.Item1))); // та же дата - интервала нет
                }
                else
                {
                    return(string.Format("{0} {1} {2} {3}", fromLabel, from.Value.ToString(formats.Item1), toLabel, to.Value.ToString(formats.Item2)));
                }
            }
            else
            {
                if (from.HasValue)
                {
                    return(string.Format("{0} {1}", fromLabel, from.Value.ToString(formats.Item1)));
                }
                else // to.HasValue
                {
                    return(string.Format("{0} {1}", toLabel, to.Value.ToString(formats.Item1)));
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Дата без года, если он равен текущему.
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string GetDateString(DateTime date)
        {
            var formats = DateFormatter.GetFormat(date, null);

            return(date.ToString(formats.Item1));
        }