转换DateTime格式c# [英] Convert DateTime Format c#

查看:127
本文介绍了转换DateTime格式c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以DateTime格式转换字符串

I would like to Convert the string in DateTime format

CultureInfo cultureInfoProvider = new CultureInfo("en-US");  
var lastUpdatedDate = updatedVAlue.Max();   //value is 20150219T045452:635599184921416348  
DateTime dt = DateTime.ParseExact(lastUpdatedDate,"yyyyMMddTHHmmss:fff",cultureInfoProvider);

我收到一个例外
字符串未被识别为有效的DateTime。 p>

I'm getting an exception String was not recognized as a valid DateTime.

推荐答案

你在这里
这将修剪你的字符串,并保持它的3位数的尾巴。

Here you are. This will trim your string and keep it's 3 digit tail.

  CultureInfo cultureInfoProvider = new CultureInfo("en-US");
  string lastUpdatedDate = updatedVAlue.Max();   //value is 20150219T045452:635599184921416348  
  string[] parse = lastUpdatedDate.Split(':');
  lastUpdatedDate = parse[0] + ":" + parse[1].Substring(0,3);
  DateTime dt = DateTime.ParseExact(lastUpdatedDate,"yyyyMMddTHHmmss:fff",cultureInfoProvider);

在此测试: https://ideone.com/EIs600

注意:如果可能,尽量避免使用C#中的 var ,在这种情况下 string 将执行此工作。

Note: If possible try to avoid using var in C#, in this case string will do the job.

这篇关于转换DateTime格式c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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