1

I'm getting this error, while trying to parse a DateTime string:

String was not recognized as a valid DateTime.

var input = "25-JUL-19 03.22.05.036000000 PM"

var output = DateTime.ParseExact(input, "dd-MMM-yy hh.mm.ss.fffffff tt", 
                                    CultureInfo.InvariantCulture, DateTimeStyles.None);

I tried it with 9 f in the format string, which leads to the same error.

Update

I find out two ways to get this to work:

Method 1:

System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
format = String.Format("dd-MMM-yy hh.mm.ss.fffffff{0} tt", 
            input.Substring(26, 2)); // substring to handle .Net's lower precision
output = DateTime.ParseExact(input, format, provider);

Method 2:

output = (DateTime)((OracleTimeStamp)inputParam.Value);

where "inputParam" is declared as:

var inputParam = new OracleParameter("SOME_TIMESTAMP", OracleDbType.TimeStamp, null, ParameterDirection.Output);
NoBullMan
  • 2,032
  • 5
  • 40
  • 93
  • 2
    The `f` custom format specifier is limited to the seven most significant digits of the seconds fraction. You're trying to parse 9 digits, which is out of scope. – Tobias Tengler Jul 25 '19 at 19:42
  • If the last 2 digits in the seconds fraction are always zero you can use `"dd-MMM-yy hh.mm.ss.fffffff00 tt"` – juharr Jul 25 '19 at 19:54
  • If you don't actually need the decimal part, you could remove it entirely, so you can also parse it with `DateTimeOffset.TryParse` or `DateTime.TryParse` – Jimi Jul 25 '19 at 20:05
  • I get this date from Oracle, a result of a SELECT statement. And I do need to store milliseconds. Also, as I mentioned, I "tried" it with 9 f's but initially I had 7 f's and it still didn't work. – NoBullMan Jul 26 '19 at 12:53

1 Answers1

1

It appears to be a limit on the f specifier. It works with 7 but fails at 8

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            var input = "25-JUL-19 03.22.05.0360000 PM";

            var output = DateTime.ParseExact(input, "dd-MMM-yy hh.mm.ss.fffffff tt", CultureInfo.InvariantCulture, DateTimeStyles.None);
        }
    }

}
Paul Baxter
  • 1,054
  • 12
  • 22
  • 1
    Since this was one of the ways to resolve the issue, I marked it as answer. It helped with coming up with method 1 in my update to the question. – NoBullMan Jul 29 '19 at 12:37