简体   繁体   English

字符串未被识别为有效的DateTime

[英]String was not recognized as a valid DateTime

I have a string 2016. I am wondering how can I parse this string to a date time? 我有一个字符串2016。我想知道如何将该字符串解析为日期时间? I have tried Datetime.ParseExact but it said it is not a valid format 我已经尝试过Datetime.ParseExact但是它说这不是有效的格式

string period = "2016"
DateTime date = DateTime.ParseExact(period, "YYYY", CultureInfo.InvariantCulture);

Help will be appreciated. 帮助将不胜感激。 Thanks 谢谢

use "yyyy" instead for YYYY then you will get the expected answer; "yyyy"代替YYYY那么您将得到预期的答案;

string period = "2016";
DateTime date = DateTime.ParseExact(period, "yyyy", CultureInfo.InvariantCulture);

You can check More DateTime Formats here 您可以在此处检查更多DateTime格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM