0

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..

3
Contributors
5
Replies
6
Views
4 Years
Discussion Span
Last Post by Mike Askew
0

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.

0

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

0
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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.