JulianJulianov

01.ObjectsAndClasses-Day of Week

Mar 5th, 2020
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. 01.ObjectsAndClasses-Day of Week
  2. You are given a date in format day-month-year. Calculate and print the day of week in English.
  3. Examples
  4. Input           Output
  5. 18-04-2016      Monday
  6. 27-11-1996      Wednesday
  7. Hints
  8. • Read the date as string from the Console.
  9. • Use the method DateTime.ParseExact(string date, format, provider) to convert the input string to object of type DateTime. Use format “d-M-yyyy” and CultureInfo.InvariantCulture.
  10. o   Alternatively split the input by-“ and you will get the day, month and year as numbers. Now you can create new DateTime(year, month, day).
  11. • The newly created DateTime object has a DayOfWeek property.
  12.  
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Globalization;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19.  
  20. namespace _01DayOfWeek
  21. {
  22.     class Program
  23.     {
  24.         static void Main(string[] args)
  25.         {
  26.             string dateAsString = Console.ReadLine();
  27.  
  28.             DateTime dateTime = DateTime.ParseExact(dateAsString, "dd-MM-yyyy", CultureInfo.InvariantCulture);
  29.  
  30.             Console.WriteLine(dateTime.DayOfWeek);
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment