Сравнение дат - C# (187289)
Формулировка задачи:
Добрый день! Нужна ваша помощь.
Задача в следующем: есть дата в формате 30 дек 2015, нужно узнать это раньше или позже, допустим, 20 окт 2015. Или сколько прошло дней с 30 дек 2015 до сегодняшнего дня. Знаю, что это нужно делать при помощи C#. Буду очень рад вашей помощи!
Решение задачи: «Сравнение дат»
textual
Листинг программы
using System;
using System.Linq;
using System.Globalization;
namespace DateCompare {
internal static class Extends {
internal static Int32 ToInt32(this String s) {
Int32 i;
if (!Int32.TryParse(s, out i)) {
throw new InvalidOperationException(
"Could not represent string as number."
);
}
return i;
}
}
internal sealed class Program {
static void Main() {
String s = "30 дек 2015";
String[] date = s.Split(' ');
DateTime dt, now = DateTime.Now;
Int32 mon = DateTimeFormatInfo
.CurrentInfo
.MonthGenitiveNames
.Select((m, i) => new {Month = m, Index = i})
.Where(m => !String.IsNullOrEmpty(m.Month)
&& m.Month.Substring(0, 3) == date[1])
.Select(i => i.Index + 1).ToArray()[0];
dt = new DateTime(date[2].ToInt32(), mon, date[0].ToInt32());
Console.WriteLine("Дата {0} больше текущей даты? {1}", s, dt > now);
}
}
}