字符串未被识别为有效的日期时间格式 [英] String was not recognized as a valid datetime format
本文介绍了字符串未被识别为有效的日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的日期为int,我从文件字段中将文件名和时间作为字符串。
我正试图获取根据字符串格式的预期输出。我收到错误,字符串未被识别为有效的日期时间格式
如有任何帮助,请提前感谢。
我尝试过:
DateTime DTTM;
int crdt = 20180418 ;
string time = 15:20: 37.6789;
DTTM = DateTime.ParseExact(crdt + + time, yyyy-MM-dd HH:mm:ss.FFFF tt,CultureInfo.InvariantCulture);
预计输出:2018-04-18 15:20:37.6789 PM
解决方案
显然你的字符串与您的格式不符。 你不想使用这种格式吗?
yyyyMMdd HH:mm:ss.FFFF
/ ravi
DateTime.ParseExact()没有接受INT作为参数的方法
System.DateTime.ParseExact < a href =https://msdn.microsoft.com/en-us/library/system.datetime.parseexact(v=vs.110).aspx> [ ^ ]
有些方法不喜欢方法中的INT,即使它在执行时被连接成一个字符串。
最好的选择是提前字符串化,也许这样可行:
DateTime DTTM;
int crdt = 20180418 ;
string time = 15:20: 37.6789;
// new
string timestring = crdt.ToString()+ time;
// 已更改
DTTM = DateTime.ParseExact(timestring , yyyyMMdd HH:mm:ss.FFFF,CultureInfo.InvariantCulture);
I have date as int, which I'm taking it from the filename and time from the file field as a string.
I'm trying to get the expected output as per the string format. I'm getting error that string was not recognized as a valid datetime format
Any help is appreciated, Thank you in advance.
What I have tried:
DateTime DTTM;
int crdt = 20180418;
string time = "15:20:37.6789" ;
DTTM = DateTime.ParseExact(crdt + " " + time, "yyyy-MM-dd HH:mm:ss.FFFF tt", CultureInfo.InvariantCulture);
Expected output : 2018-04-18 15:20:37.6789 PM
解决方案
Clearly your string doesn't match your format. Don't you want to use this format instead?
yyyyMMdd HH:mm:ss.FFFF
/ravi
DateTime.ParseExact() does not have a method that takes accepts an INT as an argument
System.DateTime.ParseExact[^]
Some methods don't like an INT within the method even though it gets concatenized into a string when executed.
Best bet would be to stringify ahead of time, perhaps this would work:
DateTime DTTM; int crdt = 20180418; string time = "15:20:37.6789" ; // new string timestring= crdt.ToString() + time; // altered DTTM = DateTime.ParseExact(timestring, "yyyyMMdd HH:mm:ss.FFFF", CultureInfo.InvariantCulture);
这篇关于字符串未被识别为有效的日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文