Click here to Skip to main content
15,921,279 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
// Online C# Editor for free
// Write, Edit and Run your C# code using C# Online Compiler

using System;
using System.Globalization;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        string strDate=Console.ReadLine();
        DateTime dateTime;
         bool   isValid = DateTime.TryParseExact(strDate, "yyyy-MM-dd",null, DateTimeStyles.None,  out dateTime);
            if (isValid){
                Console.WriteLine ("Valid date format");
            }else{
                Console.WriteLine ("Invalid date format");
            }
            string strDateC=DateTime.Now.ToString("yyyy-MM-dd");
            Console.WriteLine ("yyyy-mm-dd, Valid date format: "+strDateC);
    }
}


What I have tried:

using System;
using System.Globalization;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        string strDate=Console.ReadLine();
        DateTime dateTime;
         bool   isValid = DateTime.TryParseExact(strDate, "yyyy-MM-dd", new CultureInfo("en-GB"), DateTimeStyles.None,  out dateTime);
            if (isValid){
                Console.WriteLine ("Valid date format");
            }else{
                Console.WriteLine ("Invalid date format");
            }
            string strDateC=DateTime.Now.ToString("yyyy-MM-dd");
            Console.WriteLine ("yyyy-mm-dd, Valid date format: "+strDateC);
    }
}
Posted
Updated 5-Mar-23 20:54pm

1 solution

If I copy and paste your code into an online compiler (C# Online Compiler | .NET Fiddle[^]) and type in todays date it works fine:
C#
using System;
using System.Globalization;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
		string strDate=Console.ReadLine();
        DateTime dateTime;
         bool   isValid = DateTime.TryParseExact(strDate, "yyyy-MM-dd", new CultureInfo("en-GB"), DateTimeStyles.None,  out dateTime);
            if (isValid){
                Console.WriteLine ("Valid date format");
            }else{
                Console.WriteLine ("Invalid date format");
            }
	}
}
Hello World
2023-03-06
Valid date format
So ... what am I doing that you aren't, or vice versa?
 
Share this answer
 
Comments
itsathere 6-Mar-23 7:18am    
that's true, may due to system issue...btw thanks for your time.
OriginalGriff 6-Mar-23 7:40am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900