Javascript Regex表示日期,另一个Regex表示时间
我正在编写一个返回日期和时间(24小时)的代码。我有一个正则表达式,它与日期有效时匹配。我不知道它是否能正常工作。另外,我现在需要一个正则表达式。它应该返回格式化的日期和时间 这是一款应用程序,如果回复是c#please,我将不胜感激 谢谢你抽出时间Javascript Regex表示日期,另一个Regex表示时间,javascript,c#,regex,Javascript,C#,Regex,我正在编写一个返回日期和时间(24小时)的代码。我有一个正则表达式,它与日期有效时匹配。我不知道它是否能正常工作。另外,我现在需要一个正则表达式。它应该返回格式化的日期和时间 这是一款应用程序,如果回复是c#please,我将不胜感激 谢谢你抽出时间 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions;
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);
}
}
}
}
你可以用这个
var date = DateTime.Now.ToString("/*Here your format*/");
如果您想要24小时格式而不是忽略tt。日期的正则表达式(例如dd/mm/yyyy格式)非常复杂,我没有看到任何一个没有显示异常的表达式。 请记住,您必须根据月份和年份部分的结果来验证月份部分的日期 在一个非常复杂的正则表达式中,可能有一种方法可以做到这一点。但在代码函数中验证日期肯定更简单,也更不容易出错。 在编码年度验证之前,请注意,您的C#Date(…)函数的年份组件的下限是已知的。我在JavaScript日期(…)的年份组件中遇到了[这个奇怪的问题],并且必须相应地限制年份 时间正则表达式更容易。 使用hh:mm:ss格式的24小时时钟的粗略枚举可能类似于:
/[0][0-9]|[1][0-9]|[2][0-3]:[0-5][0-9]:[0-5][0-9]/也许您可以不使用正则表达式直接解析日期时间(包括代码片段):或此:是否显示所需格式的日期?@Md.AbdulAlim yesI建议您使用包含多个CultureInfo的TryParse链或包含多个格式的TryParseExact链。这样更好,并且执行类似的操作(根据使用上下文和回溯,Regex会有一些开销)