Hiii...

I am a beginner to c#. I want to know how to get date from the console window in c#.

As i want that on the console window user is asked to enter the date in (dd/mm/yy) format. And then i can extract the day in a variable iDay, month in variable iMon and year as iYear.

Please help to solve this problem..

thnaku..

The main question is are you going to mask the input to force that format, or simply tell them to do it and then validate it?

Masking is alot harder.

No i just want to get the date in specified format and then validate it..

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Globalization;

namespace DaniWebConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            bool ValidDate = false;
            while (!ValidDate)
            {
                Console.Write("Please enter date in format DD/MM/YYYY: ");
                string DateInput = Console.ReadLine();
                ValidDate = ValidateDate(DateInput);
            }
        }

        public static bool ValidateDate(string Date)
        {
            try
            {
                DateTime.ParseExact(Date, "dd/MM/yyyy", DateTimeFormatInfo.InvariantInfo);
                Console.WriteLine("Date Valid.");
            }
            catch
            {
                Console.WriteLine("Date Invalid.");
                return false;
            }
            return true;
        }
    }
}

Basic console app, you input a date and if its invalid it simply requests it again until it is valid.

Nice and neat solution Mikey

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.