Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# - Convert DateTime in DataRow to a formatted date string

I'm hoping that there is something I am not seeing clearly, but to simplify, I have the below code

foreach (DataRow row in dt.Rows)
{
  row["StartOn"] = Convert.ToDateTime(row["StartOn"].ToString()).ToString("MMM dd").ToString();
}

If I run the below code I get “Aug 09”

Convert.ToDateTime(row["StartOn"].ToString()).ToString("MMM dd").ToString();

If I look to see what is in row[“StartOn”] after this change it contains “8/9/2016 12:00:00 AM”

I'm unable to format my DataRow to an "MMM dd" format

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

StartOn is apparently a DateTime type. DateTime types do NOT have a format. They are an object that specifies year, month, date, and time (among other things). All you are doing in your conversions is stripping out the time so that the new datetime has a time of 12:00 am.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...