Advertisement
Krissy

CalculateDays

Jan 27th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. class CalculateDaysAlg
  6. {
  7.     static void Main()
  8.     {
  9.         Console.Write("Please enter the first date in the format dd.mm.yyyy:");//27.02.2006
  10.         string first = Console.ReadLine();
  11.         Console.Write("Please enter the second date in the format dd.mm.yyyy:");//03.03.2006
  12.         string second = Console.ReadLine();
  13.         Console.WriteLine("{0} day/s", CalculateDays(first, second));        
  14.     }
  15.     static int CalculateDays(string first, string second)
  16.     {      
  17.         DateTime firstDate = DateTime.Now;
  18.         if (ValidDate(first))
  19.         {
  20.             firstDate = DateTime.Parse(first);
  21.         }
  22.         else
  23.         {
  24.             Console.WriteLine("Please enter the data in the specified format!");
  25.            
  26.         }
  27.        
  28.         DateTime secondDate = DateTime.Now;
  29.         if (ValidDate(second))
  30.         {
  31.             secondDate = DateTime.Parse(second);
  32.         }
  33.         else
  34.         {
  35.             Console.WriteLine("Please enter the data in the specified format!");
  36.         }
  37.  
  38.         int days = (int)((secondDate - firstDate).TotalDays);
  39.         return days;
  40.     }
  41.     static bool ValidDate(string data)
  42.     {
  43.         if (Regex.IsMatch(data, "[0-9]{1,2}[.][0-9]{2,}[.][0-9]{4,}"))
  44.         {
  45.             return true;
  46.         }
  47.         else
  48.         {
  49.             return false;
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement