ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c#-以多种格式显示时建议的日期解析方式

2019-12-08 18:14:36  阅读:295  来源: 互联网

标签:date-parsing datetime c


我收集了一段时间内用户输入的日期作为字符串.由于这些来自很少或没有验证的人,因此输入的日期格式差异很大.以下是一些示例(开头的数字仅供参考):

> 1897年8月20日,21日
> 1909年5月31日,6月1日
> 2007年1月29日
> 1954年5月10日,11日,12日
> 2006年3月26日,27日,28日,29日,30日
> 2006年11月27日,28日,29日,30日,12月1日

我想在c#中解析这些日期,最后得到一组DateTime对象,每天有一个DateTime对象.因此,上面的(1)将导致2个DateTime对象,而(6)将导致5个DateTime对象.

解决方法:

我建议对它们进行一般化处理(基本上删除数字和名称,并使其成为占位符),然后按相似的格式分组,以便您可以使用示例组.

例如,1987年8月20日至21日变为[number] [postfix],[number] [postfix] [month] [year](假设将

(.*?)([0-9]{4})(?:, |$)

然后,您需要将其分解为几个月

(.*?)(January|February|...)(?:, |$)

然后,您想要该月内包含的天数:

(?:([0-9]{1,2})(?:st|nd|rd|th)(?:, )?)*(?:, |$)

然后是关于信息的汇编.但是,这只是在使用您在我面前的东西.最终,您需要知道您正在使用哪种数据以及如何处理它们.

更新

因此,我忍不住尝试自己解决这个问题.我想证明我使用的方法是准确的,而且我没有在你的裙子上吹烟.话虽如此,这就是我想出的.请注意,这在PHP中有两个原因:

> PHP更容易上手
>我认为,如果这是一个可行的解决方案,则您必须进行移植. :咧嘴:

无论如何,这是源代码和演示输出.请享用.

<?php
  $samples = array(
    '20th, 21st August 1897',
    '31st May, 1st June 1909',
    '29th January 2007',
    '10th, 11th, 12th May 1954',
    '26th, 27th, 28th, 29th, 30th March 2006',
    '27th, 28th, 29th, 30th November, 1st December 2006',
    '30th, 31st, December 2010, 1st, 2nd January 2011'
  );

  //header('Content-Type: text/plain');

  $months = array('january','february','march','april','may','june','july','august','september','october','november','december');

  foreach ($samples as $sample)
  {
    $dates = array();

    // find yearly information first
    $yearly = null;
    if (preg_match_all('/(?:^|\s)(?<month>.*?)\s?(?<year>[0-9]{4})(?:$|,)/',$sample,$yearly))
    {//var_dump($yearly);
      for ($y = 0; $y < count($yearly[0]); $y++)
      {
        $year = $yearly['year'][$y];
        //echo "year: {$year}\r\n";

        $monthly = null;
        if (preg_match_all('/(?<days>(?:(?:^|\s)[0-9]{1,2}(?:st|nd|rd|th),?)*)\s?(?<month>'.implode('|',$months).')$/i',$yearly['month'][$y],$monthly))
        {//var_dump($monthly);
          for ($m = 0; $m < count($monthly[0]); $m++)
          {
            $month = $monthly['month'][$m];
            //echo "month: {$month}\r\n";

            $daily = null;
            if (preg_match_all('/(?:^|\s)(?<day>[0-9]{1,2})(?:st|nd|rd|th)(?:,|$)/i',$monthly['days'][$m],$daily))
            {//var_dump($daily);
              for ($d = 0; $d < count($daily[0]); $d++)
              {
                $day = $daily['day'][$d];
                //echo "day: {$day}\r\n";

                $dates[] = sprintf("%d-%d-%d", array_search(strtolower($month),$months)+1, $day, $year);
              }
            }
          }
        }
        $data = $yearly[1];
      }
    }

    echo "<p><b>{$sample}</b> was parsed to include:</p><ul>\r\n";
    foreach ($dates as $date)
      echo "<li>{$date}</li>\r\n";
    echo "</ul>\r\n";
  }
?>

1897年8月20日至21日被解析为包括:
> 8-20-1897
> 8-21-1897

1909年5月31日,即1909年6月1日被解析为包括:
> 6-1-1909

2007年1月29日被解析为包括:

> 2007年1月29日

1954年5月10日,11日,12日被解析为包括:
1954年5月10日
1954年5月11日
> 5-12-1954

将2006年3月26日,27日,28日,29日,30日解析为:

> 2006年3月26日
> 2006年3月27日
> 2006年3月28日
> 2006年3月29日
> 2006年3月30日

将2006年11月27日,28日,29日,30日,12月1日解析为:
> 2006年1月1日

解析2010年12月30日,31日,2011年1月1日至2日,其中包括:
> 2010年12月30日
> 2010年12月31日
> 2011年1月1日
> 2011年1月2日

为了证明我没有袖手旁观,http://www.ideone.com/GGMaH

标签:date-parsing,datetime,c
来源: https://codeday.me/bug/20191208/2093486.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有