Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- class ReadTwoDatesCalcPeriodBetweenThem
- {
- //Write a program that reads two dates in the format: day.month.year and calculates the number of days between them. Example:
- //Enter the first date: 27.02.2006
- //Enter the second date: 3.03.2006
- //Distance: 4 days
- static DateTime ParseDate(string dateAsString)
- {
- DateTime date = DateTime.ParseExact(dateAsString, "d.M.yyyy", CultureInfo.InvariantCulture);
- return date;
- }
- static void Main()
- {
- Console.WriteLine("The format for the dates is day.month.year (e.g. 31.12.2012)");
- try
- {
- Console.Write("Enter the first date: ");
- string input = Console.ReadLine();
- DateTime startDate = ParseDate(input);
- Console.Write("Enter the second date: ");
- input = Console.ReadLine();
- DateTime endDate = ParseDate(input);
- double resultDistance = (endDate - startDate).TotalDays;
- Console.WriteLine("Distance: {0} day/s/", resultDistance);
- }
- catch (FormatException)
- {
- Console.WriteLine("Please enter the dates in the correct format 31.12.2012! Make sure that such a date exists!"); ;
- }
- catch (ArgumentNullException)
- {
- Console.WriteLine("Please provide a date. The date cannot be null!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement