C# string转换为DataTime 时间格式的几种转换方式

https://blog.csdn.net/u010082526/article/details/51733705?utm_source=blogxgwz4&utm_medium=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-2.nonecase&depth_1-utm_source=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-2.nonecase

1、C#里的SubString

  String.SubString(int   index,int   length)   

  index:开始位置,从0开始     

  length:你要取的子字符串的长度  

  获取字符,然后截取拼接

 

2、C#语言之“string格式的日期时间字符串转为DateTime类型”的方法

 

方法一:Convert.ToDateTime(string)

string格式有要求,必须是yyyy-MM-dd hh:mm:ss

================================================

方法二:Convert.ToDateTime(string, IFormatProvider)

DateTime dt;

DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo();

dtFormat.ShortDatePattern = "yyyy/MM/dd";

dt = Convert.ToDateTime("2011/05/26", dtFormat);

================================================

方法二:DateTime.ParseExact()

string dateString = "20110526";

DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

或者

DateTime dt = DateTime.ParseExact(dateString, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);

 

附参考信息:

CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US");
string format = "ddd MMM d HH:mm:ss zz00 yyyy";
string stringValue = DateTime.Now.ToString(format, cultureInfo); // 得到日期字符串
DateTime datetime = DateTime.ParseExact("Wed Aug 25 16:28:03 +0800 2010", format, cultureInfo); // 将字符串转换成日期
posted on 2020-10-29 19:48  伪电气白兰地  阅读(10683)  评论(0编辑  收藏  举报