怎样将string类型转换成yyyy-MM-dd hh:mm:ss格式

qq_30209611 2016-12-13 10:38:25
要将“2016年12月“转换成yyyy-MM-dd hh:mm:ss格式,再与数据表内的列进行比较,怎么可以成功转化过了?
...全文
2059 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
XBodhi. 2017-02-24
  • 打赏
  • 举报
回复
    DateTime dt = DateTime.ParseExact("2016年12月", "yyyy年MM月", System.Globalization.CultureInfo.InvariantCulture);
https://msdn.microsoft.com/zh-cn/library/w2sa9yss.aspx
正怒月神 2017-02-04
  • 打赏
  • 举报
回复
string d = "2016年12月";
            string d1 = DateTime.Parse(d).ToString("yyyy-MM-dd HH:mm:ss");
zh_908952047 2017-02-03
  • 打赏
  • 举报
回复
我用vs2013处理了下没问题,应该是你版本的问题
睡觉的…… 2016-12-17
  • 打赏
  • 举报
回复
var dt = DateTime.ParseExact("2016年12月", "yyyy年MM月", CultureInfo.DefaultThreadCurrentCulture)
xiajing12345 2016-12-13
  • 打赏
  • 举报
回复
string s = "2016年12月1日"; DateTime dt = DateTime.ParseExact("2016年12月", "yyyy年MM月", CultureInfo.DefaultThreadCurrentCulture); String sout = dt.ToString("yyyy-MM-dd hh:mm:ss");
li905663280 2016-12-13
  • 打赏
  • 举报
回复
In the .NET Framework 4 and previous versions, by default, the culture of all threads is set to the Windows system culture. For applications whose current culture differs from the default system culture, this behavior is often undesirable. In the .NET Framework 4.5, the DefaultThreadCurrentCulture property enables an application to define the default culture of all threads in an application domain.
qq_30209611 2016-12-13
  • 打赏
  • 举报
回复
引用 3 楼 starfd 的回复:
你没添加引用吧?这个是类,不是属性
就是添加了System.Globalization这个类,才有cultrueinfo这个方法,但这个方法里面没有后面那个属性
  • 打赏
  • 举报
回复
你没添加引用吧?这个是类,不是属性
qq_30209611 2016-12-13
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
var dt = DateTime.ParseExact("2016年12月", "yyyy年MM月", CultureInfo.DefaultThreadCurrentCulture)
转化为DateTime后,随便你怎么转成其它格式字符串了
cultrueinfo里面没有后面那个属性啊?
  • 打赏
  • 举报
回复
var dt = DateTime.ParseExact("2016年12月", "yyyy年MM月", CultureInfo.DefaultThreadCurrentCulture)
转化为DateTime后,随便你怎么转成其它格式字符串了
qq_30209611 2016-12-13
  • 打赏
  • 举报
回复
引用 8 楼 starfd 的回复:
用CultureInfo.InvariantCulture这个吧 那个是4.5之后才有的
我上午也用这个定义,var dt = DateTime.ParseExact(szTime, "yyyy年MM月", CultureInfo.InvariantCulture),不过这样运行会中断,是因为我改变了第一个参数的值吗,这个需要转的时间不是常量,是一个变量。
  • 打赏
  • 举报
回复
用CultureInfo.InvariantCulture这个吧 那个是4.5之后才有的
qq_30209611 2016-12-13
  • 打赏
  • 举报
回复
引用 6 楼 xiajing12345 的回复:
string s = "2016年12月1日"; DateTime dt = DateTime.ParseExact("2016年12月", "yyyy年MM月", CultureInfo.DefaultThreadCurrentCulture); String sout = dt.ToString("yyyy-MM-dd hh:mm:ss");
cultureinfo里面没有后面那个定义,添加了System.Globalization这个类,难道是我用vs2010的原因?

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧