Skip to content
Loading
  • Hemant Jindal
    0
    1. try  
    2. {  
    3.     Console.Write("Please enter date: ");  
    4.     var dateFrom = Console.ReadLine();  
    5.     DateTime formatedDate = DateTime.ParseExact(dateFrom, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);  
    6.     Console.WriteLine("Date in format {0} - {1} ", "dd//MM/yyyyHH:mm", formatedDate);  
    7. }  
    8. catch (Exception e)  
    9. {  
    10.     Console.WriteLine(e);  
    11. }  
    12. Console.ReadLine();  
    Now if you enter date 25/08/2008, this will given an exception because we have not provided time in the given date
     
    System.FormatException: String was not recognized as a valid DateTime.
    at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
    at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)
    at StringAlogrithms.Program.Main(String[] args) in C:\Personal Data\Projects\StringAlogrithms\StringAlogrithms\Program.cs:line 31
     
    correct date : 25/08/2008 -  this will give the expected result Date in format dd//MM/yyyyHH:mm - 25-Aug-08 2:05:00 PM 
     
    Please accept this answer if you find helpful 
    0