Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My .csv file have a date "2014-11-26 15:46:56.107000000" in this formate. and i am trying to use DateTime.ParseExact after replacing the "-" for "/"



C#
class Program
   {

       public static DateTime GetDateTimeFromString(String date)
       {
           return DateTime.ParseExact(date, "dd/MM/yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture);
       }
       static void Main(string[] args)
       {
       DateTime S=   Program.GetDateTimeFromString("26/11/2014 15:46:56.107000000");
           Console.WriteLine(S);
           Console.ReadLine();

           // "26/11/2014 15:46:56.107000000"



       }
   }


then there is Exception occur "String was not recognized as a valid DateTime"
Posted

your input string having many trailing zeros, you may need to remove them like below

C#
string input  ="26/11/2014 15:46:56.107000000";
if(input.Length >23)
   input = input.Substring(0,23);
DateTime S=   Program.GetDateTimeFromString(input);
 
Share this answer
 
Your error is caused by the fact that the fraction-of-a-second part of your date string has more than seven places. If you reduce the number of letters after the seconds to seven, it will work, as you can see here:
C#
// required
using System.Globalization;

private void testConversion()
{
    string date = "2014-11-26 15:46:56.1070000";
    
    string inFormat = "yyyy-MM-dd HH:mm:ss.FFFFFFF";
    string outFormat = "dd/MM/yyyy HH:mm:ss.FFFFFFF";
    
    DateTime result;
    result = DateTime.ParseExact(date, inFormat, CultureInfo.InvariantCulture);
    
    string reformat = (result.ToString(outFormat)); // => "26/11/2014 15:46:56.107"
}
So, you can create some strategy to make sure the fractional-second part of your string is a maximum of seven characters long; something like this:
C#
string date = "2014-11-26 15:46:56.107123456789";

string secFrac = date.Substring(date.IndexOf('.') + 1);

if (secFrac != date) // there is a fractional seconds part
{
    if (secFrac.Length > 7)
    {

        date = date.Replace(secFrac, secFrac.Substring(0, 7));
    }
}
 
Share this answer
 
try to give like below

C#
class Program
   {

       public static DateTime GetDateTimeFromString(String date)
       {
           return DateTime.ParseExact(date, "dd/MM/yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture);
       }
       static void Main(string[] args)
       {
       DateTime S=   Program.GetDateTimeFromString("26/11/2014 15:46:56.107");
           Console.WriteLine(S);
           Console.ReadLine();

           // "26/11/2014 15:46:56.107000000"



       }
   }
 
Share this answer
 
v4
Why aren't you using Convert.ToDateTime()

string str = "2014-11-26 15:46:56.107000000";
DateTime dt = Convert.ToDateTime(str);


This is simply working.
 
Share this answer
 

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