Read date time value from console - CSharp Language Basics

CSharp examples for Language Basics:Console

Description

Read date time value from console

Demo Code

using System;/*  ww w . j av a 2s  .c  o m*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
   static void Main(string[] args)
   {
      // Date input
      Console.Write("Enter a date: ");
      string input = Console.ReadLine();
      DateTime enteredDate = Convert.ToDateTime(input);
      // Calculations
      int enteredYear = enteredDate.Year;
      int enteredMonth = enteredDate.Month;
      DateTime firstDayOfMonth = new DateTime(enteredYear, enteredMonth, 1);
      DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
      Console.WriteLine("Corresponding month: " + "from " + firstDayOfMonth.ToShortDateString() + " to " + lastDayOfMonth.ToShortDateString());
   }
}

Result


Related Tutorials