Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

c# - How to Convert Stringified Date Value in format Month/Day/Year Time as a DateTime?

I've got a class that contains two DateTime members.

When I try to assign a "human-readable" value representing a date to the DateTime member, though, it rejects it with the message, "String was not recognized as a valid DateTime."

The line of code that fails is:

md.BeginDate = DateTime.Parse(reader.GetValue(5).ToString());

In context, showing the value that I am trying to convert from a string representation of a DateTime:

enter image description here

Based on the 1st and accepted answer here [https://stackoverflow.com/questions/2193012/string-was-not-recognized-as-a-valid-datetime-format-dd-mm-yyyy], I also tried this:

md.BeginDate = DateTime.ParseExact(reader.GetValue(6).ToString(), "MM/dd/yyyy", null); 

...but with the same ignominious result.

UPDATE

Based on this, I tried the following:

CultureInfo provider = CultureInfo.InvariantCulture;
string format = "yyyy-MM-dd HH:mm:ss.ffffff";
. . .
DateTime.ParseExact(dateString, format, provider);

...but get the same result.

The value of "dateValAsStr" is 2021-01-21 11:25:56.9608384

UPDATE 2

It turns out it was a really dumb oversight on my part. When I stepped through it, I saw that the Date (TEXT) value in the database was empty. So this code "works" (doesn't throw an exception):

CultureInfo provider = CultureInfo.InvariantCulture;
string format = "yyyy-MM-dd HH:mm:ss.fffffff";
. . .
string qry = "SELECT LocationName, Address1, City, StateOrSo, PostalCode, " +
                        "BeginDate, EndDate, MapDetailNotes, Latitude, Longitude " +
                        "FROM CartographerDetail " +
                        "WHERE FKMapName = @FKMapName";
. . .
dateValAsStr = reader.GetValue(5).ToString().Trim();
if (! String.IsNullOrWhiteSpace(dateValAsStr))
{
    md.BeginDate = DateTime.ParseExact(dateValAsStr, format, provider).Date;
}
dateValAsStr = reader.GetValue(6).ToString().Trim();
if (!String.IsNullOrWhiteSpace(dateValAsStr))
{
    md.EndDate = DateTime.ParseExact(dateValAsStr, format, provider).Date;
}
question from:https://stackoverflow.com/questions/65946734/how-to-convert-stringified-date-value-in-format-month-day-year-time-as-a-datetim

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The simple example you give is this:

string dateString = "2021-01-21 11:25:56.9608384";
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "yyyy-MM-dd HH:mm:ss.fffffff";
DateTime result = DateTime.ParseExact(dateString, format, provider);

Note that the number of decimal places for the seconds is 7, yet your format only has 6 fs.

If you change to 7 fs then it works fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...