C#如何字符串转换成时间和日期格式? [英] C# How to convert String into Time and Date format?

查看:1227
本文介绍了C#如何字符串转换成时间和日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简短的计划,将字符串转换为日期和时间格式,从一个简单的字符串。



但是似乎字符串不recorgnizable为系统将被转换成日期时间格式由于字符串的序列。应该转换的字符串就像是一个例子:周四12月9日五时12分42秒2010



的方法Convert.ToDateTime 已被使用,但不起作用。



可能有人请告知代码? !谢谢



 字符串重新=周四12月9日5时12分42秒2010; 

DateTime的时间= Convert.ToDateTime(重新);

Console.WriteLine(time.ToString(DDDD,DD MMMM YYYY HH:MM:SS));


解决方案

经常需要给它一个关于提示您所期望的特定模式:



编辑:双空间是一种痛苦,因为 D 不处理是什么;

  DateTime的时间= DateTime.ParseExact(re.Replace(,),
DDD年MMM d HH:MM:SS YYYY,CultureInfo.CurrentCulture);


I have a short program which converts a string to both date and time format from a simple string.

However it seems that the String is not recorgnizable for the system to be converted into date time format due to the sequence of the string. The String that should be converted is an example like : "Thu Dec 9 05:12:42 2010"

The method of Convert.ToDateTime have been used but does not work.

May someone please advise on the codes? Thanks!

String re = "Thu Dec  9 05:12:42 2010";

DateTime time = Convert.ToDateTime(re);

Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));

解决方案

It is often necessary to give it a hint about the specific pattern that you expect:

Edit: the double-space is a pain, as d doesn't handle that;

DateTime time = DateTime.ParseExact(re.Replace("  "," "),
     "ddd MMM d hh:mm:ss yyyy", CultureInfo.CurrentCulture);

这篇关于C#如何字符串转换成时间和日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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