C# 在C语言中将字符串转换为日期#

C# 在C语言中将字符串转换为日期#,c#,C#,我想知道如何将一个四到六位数的字符串转换成C#中的日期 111110将是11110 11110将是11110 110将是1110 模式为mmddyymmd-yymd-yy 空格不是'就是'\0'(我得到的输入不是很干净) 这就是我目前为止所做的,它适用于上述所有情况,只是不太好看: 上述情况是否有更有效的解决方案 //Converts the given input string into a valid Date private DateTime convertToDateFromString

我想知道如何将一个四到六位数的字符串转换成C#中的日期

111110将是11110

11110将是11110

110将是1110

模式为
mmddyy
mmd-yy
md-yy
空格不是
'
就是
'\0'
(我得到的输入不是很干净)

这就是我目前为止所做的,它适用于上述所有情况,只是不太好看: 上述情况是否有更有效的解决方案

//Converts the given input string into a valid Date
private DateTime convertToDateFromString(string dateString)
{
  int length = dateString.Length;
  int month = 1;
  int day = 1;
  int year = 1;
  bool gotMonth = false;
  bool gotDay = false;
  bool gotYear = false;
  char c = ' ';
  char peek = ' ';
  string buffer = "";
  DateTime bufferDate;
  int count = 0;

  try
  {
    //loop character by character through the string
    for (int i = 0; i < dateString.Length; i++)
    {
      c = dateString[i];
      if ((i + 1) < dateString.Length)
        peek = dateString[i + 1];
      else
        peek = '\0';

      if (c != ' ' && c != '\0')
      {
        buffer += c;
        count++;
        //Check if the month is done
        if ((peek == ' ' || peek == '\0' || count == 2) && gotMonth == false)
        {
          count = 0;
          gotMonth = true;
          month = int.Parse(buffer);
          buffer = null;
        }
        //Check if the day is done
        else if ((peek == ' ' || peek == '\0' || count == 2) && gotDay == false && gotMonth == true)
        {
          count = 0;
          gotDay = true;
          day = int.Parse(buffer);
          buffer = null;
        }
        //Check if the year is done
        else if ((peek == ' ' || peek == '\0' || count == 2) && gotYear == false && gotMonth == true && gotDay == true)
        {
          count = 0;
          gotYear = true;
          year = int.Parse(buffer);
          buffer = null;

          if (year >= 80 && year <= 99)
            year += 1900;
          else if (year >= 0 && year <= 79)
            year += 2000;
        }
      }
    }
    bufferDate = new DateTime(year, month, day);
  }
  catch (System.Exception ex)
  {
    bufferDate = new DateTime(1, 1, 1);
  }
  return bufferDate;
}
//将给定的输入字符串转换为有效日期
私有DateTime convertToDateFromString(字符串dateString)
{
int length=dateString.length;
整月=1;
整日=1;
整年=1;
bool gotmount=false;
bool-gotDay=false;
bool-gotYear=false;
字符c='';
char-peek='';
字符串缓冲区=”;
日期时间缓冲日期;
整数计数=0;
尝试
{
//在字符串中逐字符循环
for(int i=0;i如果(年份>=80&&year=0&&year您应该使用在
DateTime
上定义的许多
Parse
方法之一

这些将采用字符串、可选格式字符串(描述日期时间字符串的格式)和可选区域性

看一看,其中一些将采用
string[]
的格式进行尝试

此外,这里还有可用的字符串格式以及日期和时间格式字符串。

尝试使用

DateTime.TryParseExact(dateString, new string[] { "MMddyy", "MMd yy", "M d yy" }, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out result);
如果你需要,你可以

dateString = dateString.Replace('\0', ' ');

在开始之前,您需要识别器这里是空格数。首先获取:

string source = "....";    
int spaceCount = source.Count(c => c == ' ');
然后为预期范围0..2创建FormatString。您可以使用问题中的字符串,但月份必须使用M:

var formats = new string[] { "MMddyy", "MMd yy", "M d yy" };
然后你可以得到你的约会对象:

DateTime r = DateTime.ParseExact(source, formats[spaceCount], null);

根据需要添加验证。

是否有可接受的
DateTime.Parse
字符串格式列表?@NexAddo-是的,有。答案已修改。问题是他需要使用许多格式字符串。@McKay-一些解析方法采用
string[]
尝试不同的格式。您似乎从回答中了解到了这些格式。@Oded ParseExact方法可以,而不是Parse方法。(因此,我的回答实际上采用了一系列格式)这是一个更有效的解决方案。谢谢!这就是当你没有对可用函数做足够的研究并尝试从头开始做事情时会发生的情况……是我还是你刚刚为一个一小时前已经结束的问题重写了一个新问题?