C# mysql insert command and converting ISO 8601 Time string
What i have is a string that is ISO 8601 and I'm trying to insert it into a MySql table. The table takes the date fine but does 开发者_JS百科not recognize the time.
string myTime = "3/8/2011 5:36:54 PM" (this comes in ISO 8601 format). string mytimeFormat = myTime.ToString("yyyy-MM-dd HH:MM:SS");
and a basic insert into mysql statement. The MySql table shows the date correctly, just not the time. Any tips?
When i convert it using the string mytimeFormat it looks like '2011-03-08 17:36:54' This is where MySql accepts the date just not the time.
You will need to convert the string to a DateTime first, so the formatting can done properly.
string myTime = "3/8/2011 5:36:54 PM"; //(this comes in ISO 8601 format).
DateTime myDateTime = DateTime.Parse(myTime);
string mytimeFormat = myDateTime.ToString("yyyy-MM-dd HH:MM:SS");
Update: In the format, a uppercase M
is the month placeholder, use a lowercase m
to get the minute:
myDateTime.ToString("yyyy-MM-dd HH:mm:SS")
Make sure you watch the case of your formatting string. If using ToString from a DateTime object make sure you use:
myDateTime.ToString("yyyy-MM-dd hh:mm:ss");
You might also want to make sure any parsing you are doing is actually working before checking the output from the ToString.
精彩评论