日期字符串转换为日期格式 [英] converting date string to date format
问题描述
我已经尝试了许多建议,我#2发现,但没有得到期望的结果。 。任何帮助将非常感激。
I have tried out as many suggestions as I've found on Stackoverflow, but not getting the desired result. Any help would be much appreciated.
我的日期字符串为04 - 12月2013 14:14:02.143,我想这个转变正是因为成<$ C 。$ C>的DateTime 格式
My date string is "04-Dec-2013 14:14:02.143" and I want to convert this exactly as into DateTime
format.
这是最后一个建议,我尝试:
This was the last suggestion I tried:
String MyString;
MyString = "04-Dec-2013 14:14:02.143";
DateTime MyDateTime;
MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "dd-MMM-yyyy HH:mm:ss.fff",
null);
不过,我不断收到的2013年4月12日14时14分02秒的令人失望的结果而是希望它是04 - 12月2013 14:14:02.143。
However, I keep getting the undesired result of "04/12/2013 14:14:02" rather want it to be "04-Dec-2013 14:14:02.143".
任何建议
推荐答案
是的,你应该阅读有关的DateTime
结构。 它不会有任何格式的信息连接,它只是表示时间点一个普通的数字。
Yes, you should read about DateTime
struct. It has not have any format info attached, it's just a plain number representing point in time.
格式开始发挥作用,当您尝试获取数据的字符串
表示,使用的ToString(格式)
方法。
The format come into play when you try to get string
representation of the data, using ToString(format)
method.
使用格式字符串每次你打电话时间的ToString
来得到你希望它是格式的日期:
Use the format string every time you're calling ToString
to get the date in the format you want it to be:
var stringDateRespresentation = dateValue.ToString("dd-MMM-yyyy HH:mm:ss.fff");
为方便起见你应该通过普通,非格式化的的DateTime
实例各地,把它变成字符串
使用的ToString
,只有当它被呈现给用户的方法
To make things easier you should pass plain, non-formatted DateTime
instances all around and change it into string
using ToString
method only when it's being presented to the user.
这篇关于日期字符串转换为日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!