如何将sting转换为datetime格式,如MM / dd / yyyy [英] How to convert sting to datetime format like MM/dd/yyyy
问题描述
我得到字符串值像01/05/2012(MM / dd / yyyy),现在我必须转换为datetime
i get the string value like 01/05/2012(MM/dd/yyyy) , now i have to convert into datetime
string s="01/05/2012";
datetime dt=convert.todatetime(s);
现在dt值为1/5/2012,但我必须得到dt值01/05 /
Now dt value is 1/5/2012, but i have to get dt value 01/05/2012, how to get the value , plese give me any idea about that.
感谢
hemanth
Thanks hemanth
推荐答案
您正在创建 DateTime
对象,您只是查看DateTime的内部表示。输出方式取决于您向其 ToString
方法。
ie:
You are creating your DateTime
object correctly, you are just looking at the internal representaion of the DateTime. The way that it is output depends on the Format String you give its ToString
Method.
i.e.:
class Program
{
static void Main(string[] args)
{
string s = "01/05/2012";
DateTime dt = Convert.ToDateTime(s);
Console.WriteLine(dt.ToString("MM/dd/yyyy"));
Console.ReadLine();
}
}
如果您查看日期时间结构
从上面的链接突出显示我
From above link highlighting mine
DateTime值及其字符串表示
DateTime Values and Their String Representations
在内部,所有DateTime值表示为从0001年1月1日午夜12:00:00起经过的刻度数(100纳秒间隔的数量)。实际的DateTime值独立于该值在用户界面元素中显示时或在写入文件时出现的方式。 DateTime值的外观是格式化操作的结果。格式是将值转换为其字符串表示形式的过程。
Internally, all DateTime values are represented as the number of ticks (the number of 100-nanosecond intervals) that have elapsed since 12:00:00 midnight, January 1, 0001. The actual DateTime value is independent of the way in which that value appears when displayed in a user interface element or when written to a file. The appearance of a DateTime value is the result of a formatting operation. Formatting is the process of converting a value to its string representation.
因为日期和时间值的出现取决于诸如文化,国际标准,应用要求和个人偏好等因素。 DateTime结构通过其ToString方法的重载提供了格式化日期和时间值的大量灵活性。默认的DateTime.ToString()方法使用当前文化的短日期和长时间模式返回日期和时间值的字符串表示形式。
Because the appearance of date and time values is dependent on such factors as culture, international standards, application requirements, and personal preference, the DateTime structure offers a great deal of flexibility in formatting date and time values through the overloads of its ToString method. The default DateTime.ToString() method returns the string representation of a date and time value using the current culture's short date and long time pattern.
这篇关于如何将sting转换为datetime格式,如MM / dd / yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!