nik
nik

Reputation: 1784

Convert to datetime from Oracle

i know there are a lot of similar questions, but I couldn't find what I was looking for.

Here is my oracle date:

string testdate= "2014-01-07 15:00:00.0000000";

And here is how I tried to convert to datetime:

DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)

This throws a format exception. Any ideas?

My quick test also throws the string not valid datetime exception. Quick test:

Console.WriteLine(DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture).ToShortDateString());    

Upvotes: 0

Views: 1278

Answers (2)

Mike Hixson
Mike Hixson

Reputation: 5189

Why use ParseExact at all? Reqular Parse seems to work.

var dt = DateTime.Parse("2014-01-07 15:00:00.0000000", CultureInfo.InvariantCulture);

// Prints out 2014-01-07T15:00:00.0000000
Console.WriteLine(dt.ToString("o"));

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

I'd start by trying to avoid getting it as a string in the first place. Make sure you're using the appropriate data type in Oracle, and you should be able to call GetDateTime on the appropriate DataReader (or whatever you're using).

If you must parse it as text, then you need to specify a format which matches the value - so use 7 fs instead of 3, given that your value has ".0000000" at the end.

DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fffffff",
                    CultureInfo.InvariantCulture)

But again, I'd strongly urge you to avoid having to deal with the value as text at all.

Upvotes: 3

Related Questions