如何将 sting 转换为日期时间格式,如 MM/dd/yyyy [英] How to convert sting to datetime format like MM/dd/yyyy

查看:28
本文介绍了如何将 sting 转换为日期时间格式,如 MM/dd/yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到像 01/05/2012(MM/dd/yyyy) 这样的字符串值,现在我必须转换成日期时间

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,但我必须获得 01/05/2012 的 dt 值,如何获得该值,请给我任何想法.

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.

谢谢汉斯

推荐答案

您正在正确创建 DateTime 对象,您只是在查看 DateTime 的内部表示.它的输出方式取决于您为其提供的格式字符串 ToString 方法.即:

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();
    }
}

<小时>

如果您在 日期时间结构上查看此 MSDN 页面


If you look at this MSDN Page on the DateTime Structure

从上面的链接突出我的

日期时间值及其字符串表示

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 结构通过重载在格式化日期和时间值方面提供了很大的灵活性 默认的 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 转换为日期时间格式,如 MM/dd/yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆