当前位置:编程学习 > C#/ASP.NET >>

求时间yyyyMMddhhmmss格式的正则表达式

求时间yyyyMMddhhmmss格式的正则表达式,谢谢! --------------------编程问答-------------------- http://hi.baidu.com/phanchun/blog/item/8bb0ef09c1d62222e82488c6.html --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace sxLdfang
{
    class Program
    {
        static void Main(string[] args)
        {
            string html = @"20110822091056";
            string pattern = @"(?=\d{14})(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})(?<hour>\d{2})(?<minite>\d{2})(?<second>\d{2})";
            MatchCollection mc = Regex.Matches(html, pattern);
            foreach (Match m in mc)
            {
                Console.WriteLine("      year:" + m.Groups["year"].Value);
                Console.WriteLine("     month:" + m.Groups["month"].Value);
                Console.WriteLine("       day:" + m.Groups["day"].Value);
                Console.WriteLine("      hour:" + m.Groups["hour"].Value);
                Console.WriteLine("    minite:" + m.Groups["minite"].Value);
                Console.WriteLine("    second:" + m.Groups["second"].Value);
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}


运行结果:
      year:2011
     month:08
       day:22
      hour:09
    minite:10
    second:56


然后对个变量的值进行判断。 --------------------编程问答-------------------- tostring("yyMMddhhmmss"); --------------------编程问答--------------------  string fname = System.DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + Session.SessionID.ToString();

运行出来是:2011-08-22-09-35-31


以此类推. --------------------编程问答--------------------
引用 4 楼 xinlin2009 的回复:
 string fname = System.DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + Session.SessionID.ToString();

运行出来是:2011-08-22-09-35-31


以此类推.

额 错了  
string fname = System.DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") +Session.SessionID.ToString();
对应的是:2011-08-22-09-35-31nw30i0ex54g4bxih4x2vkayf

这个才是string fname = System.DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss");
2011-08-22-09-35-31 --------------------编程问答-------------------- c#的没有用过。js的倒是有。

return /(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)/.test(str);

因为要判断闰年、大小月等等。
在c#下面我一般try...catch一下Convert.ToDateTime来处理。 --------------------编程问答--------------------
引用 6 楼 xustanly 的回复:
c#的没有用过。js的倒是有。
JScript code

return /(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(3……



就是要js的,不过好像不行哦,,要yyyyMMddhhmmss格式啊,如20110822110101 --------------------编程问答-------------------- 这个确实只到年月日,时分秒得自己加了. --------------------编程问答--------------------
引用 8 楼 xustanly 的回复:
这个确实只到年月日,时分秒得自己加了.
  能帮我加上么??就是不会才上来问的啊~ --------------------编程问答--------------------
^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$

显示为: yyyy-MM-dd hh:mm:ss 
如果不要短横杠,自己改。 --------------------编程问答-------------------- DateTime.Now.ToString("yyyyMMddhhmmss")
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,