Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write a program that enters two dates in format dd.MM.yyyy and returns the number of days between them.
- namespace _04.DifferenceBetweenDates
- {
- using System;
- using System.Globalization;
- using System.Threading;
- public class DifferenceBetweenDates
- {
- public static void Main(string[] args)
- {
- checked
- {
- Console.WriteLine("The Date must enters exactly like this format [dd.MM.yyyy] !");
- Console.Write("Enter the First date: ");
- DateTime firstDate = ReadStringFromInputAndConvertToDateTimeFormat();
- Console.Write("Enter the Second date: ");
- DateTime secondDate = ReadStringFromInputAndConvertToDateTimeFormat();
- int daysBetweenDates = CalculateDaysBetweenTwoDates(firstDate, secondDate);
- Console.WriteLine("Days Between these two Dates are: {0} !", daysBetweenDates);
- }
- }
- private static int CalculateDaysBetweenTwoDates(DateTime firstDate, DateTime secondDate)
- {
- checked
- {
- TimeSpan diff = secondDate - firstDate;
- int days = diff.Days;
- return days;
- }
- }
- private static DateTime ReadStringFromInputAndConvertToDateTimeFormat()
- {
- checked
- {
- string inputStr = Console.ReadLine();
- string dateTimeFormatStr = "d.MM.yyyy";
- DateTime date = DateTime.ParseExact(inputStr, dateTimeFormatStr, CultureInfo.InvariantCulture);
- return date;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement