用于日期的正则表达式和用于时间的另一个正则表达式

时间:2018-07-31 07:18:36

标签: javascript c# regex

我正在编写一个返回日期和时间(24小时)的代码。我有一个正则表达式可以在日期有效时匹配。我不完全知道它是否正常运行。另外,我现在需要正则表达式。应该返回格式化的日期和时间。

这是一个应用程序,因此,如果响应是c#,请多谢。

感谢您抽出宝贵的时间。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

public class Example
{
 public static void Main()
   {
     string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", 
                        "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", 
                        "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", 
                        "M/d/yyyy h:mm", "M/d/yyyy h:mm", 
                        "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
                        "MM/d/yyyy HH:mm:ss.ffffff", 

                        "M-d-yyyy h:mm:ss tt", "M-d-yyyy h:mm tt", 
                        "MM-dd-yyyy hh:mm:ss", "M-d-yyyy h:mm:ss", 
                        "M-d-yyyy hh:mm tt", "M-d-yyyy hh tt", 
                        "M-d-yyyy h:mm", "M-d-yyyy h:mm", 
                        "MM-dd-yyyy hh:mm", "M-dd-yyyy hh:mm",
                        "MM-d-yyyy HH:mm:ss.ffffff" };

  string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", 
                          "5/1/2009 6:32:00", "05/01/2009 06:32", 
                          "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00",
                          "08/28/2015 16:17:39.125", "08/28/2015 
                           16:17:39.125000",

                          "5-1-2009 6:32 PM", "05-01-2009 6:32:05 PM", 
                          "5-1-2009 6:32:00", "05-01-2009 06:32", 
                          "05-01-2009 06:32:00 PM", "05-01-2009 06:32:00",
                          "08-28-2015 16:17:39.125", "08-28-2015 
                           16:17:39.125000" };
                           DateTime dateValue;

  string pattern = @"(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:1|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})";
  Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);  
  MatchCollection matches = rgx.Matches(input);
     if (matches.Count > 0)
{
  foreach (string dateString in dateStrings)
  {
     try {
        dateValue = DateTime.ParseExact(dateString, formats, new
                          CultureInfo("en-US"), DateTimeStyles.None);
        Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
         }
     catch (FormatException) { 
        Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
        }
      }
   }
}

2 个答案:

答案 0 :(得分:0)

您可以使用此

var date = DateTime.Now.ToString("/*Here your format*/");

如果要使用24小时格式,请忽略tt。

答案 1 :(得分:0)

日期的正则表达式,例如格式为 dd / mm / yyyy 的代码非常复杂,我还没有看到其中没有任何一个例外。 请记住,您必须根据月份和年份部分的结果来验证月份的一部分。 。 。

在非常复杂的正则表达式中可能有(?)一种方法。但是,在代码函数中验证日期肯定更简单且更不容易出错。 在对年验证进行编码之前,请注意C#Date(...)函数的年部分的下限是已知的。我在JavaScript Date(...)的1-99范围内遇到了[这个奇怪的问题] [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Parameters],并且不得不相应地限制年份。

时间正则表达式更容易。 带有 hh:mm:ss 格式的24小时时钟的粗略枚举可能类似于:

/ [0] [0-9] | [1] [0-9] | [2] [0-3]:[0-5] [0-9]:[0-5] [0-9] /