@@ -121,6 +121,21 @@ public MyDate(decimal num)
121121 }
122122 }
123123
124+
125+ private static readonly Regex dateTimeRegex = new Regex ( "^(\\ d{4})-(1[012]|0?\\ d)-(30|31|[012]?\\ d) ([01]?\\ d|2[0123]):([012345]?\\ d):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
126+ private static readonly Regex dateTimeRegex2 = new Regex ( "^(\\ d{4})/(1[012]|0?\\ d)/(30|31|[012]?\\ d) ([01]?\\ d|2[0123]):([012345]?\\ d):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
127+ private static readonly Regex dateTimeRegex3 = new Regex ( "^(\\ d{4})-(1[012]|0?\\ d)-(30|31|[012]?\\ d) ([01]?\\ d|2[0123]):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
128+ private static readonly Regex dateTimeRegex4 = new Regex ( "^(\\ d{4})/(1[012]|0?\\ d)/(30|31|[012]?\\ d) ([01]?\\ d|2[0123]):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
129+
130+ private static readonly Regex dateRegex = new Regex ( "^(\\ d{4})-(1[012]|0?\\ d)-(30|31|[012]?\\ d)$" , RegexOptions . Compiled ) ;
131+ private static readonly Regex dateRegex2 = new Regex ( "^(\\ d{4})-(1[012]|0?\\ d)-(30|31|[012]?\\ d)$" , RegexOptions . Compiled ) ;
132+
133+ private static readonly Regex dayTimeRegex = new Regex ( "^(\\ d+) (2[0123]|[01]?\\ d):([012345]?\\ d):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
134+ private static readonly Regex dayTimeRegex2 = new Regex ( "^(\\ d+) (2[0123]|[01]?\\ d):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
135+
136+ private static readonly Regex timeRegex = new Regex ( "^(2[0123]|[01]?\\ d):([012345]?\\ d):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
137+ private static readonly Regex timeRegex2 = new Regex ( "^(2[0123]|[01]?\\ d):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
138+
124139 /// <summary>
125140 /// 字符串转MyDate
126141 /// </summary>
@@ -130,10 +145,8 @@ public static MyDate Parse(String txt)
130145 {
131146 CultureInfo cultureInfo = CultureInfo . InvariantCulture ; // CultureInfo.GetCultureInfo("zh-cn");
132147 String t = txt . Trim ( ) ;
133- var m = Regex . Match ( t , "^(\\ d{4})-(1[012]|0?\\ d)-(30|31|[012]?\\ d) ([01]?\\ d|2[0123]):([012345]?\\ d):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
134- if ( m . Success == false ) {
135- m = Regex . Match ( t , "^(\\ d{4})/(1[012]|0?\\ d)/(30|31|[012]?\\ d) ([01]?\\ d|2[0123]):([012345]?\\ d):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
136- }
148+ var m = dateTimeRegex . Match ( t ) ; // 年月日 时分秒
149+ if ( m . Success == false ) { m = dateTimeRegex2 . Match ( t ) ; }
137150 if ( m . Success ) {
138151 MyDate date = new MyDate {
139152 Year = int . Parse ( m . Groups [ ( 1 ) ] . Value , cultureInfo ) ,
@@ -145,10 +158,9 @@ public static MyDate Parse(String txt)
145158 } ;
146159 return date ;
147160 }
148- m = Regex . Match ( t , "^(\\ d{4})-(1[012]|0?\\ d)-(30|31|[012]?\\ d) ([01]?\\ d|2[0123]):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
149- if ( m . Success == false ) {
150- m = Regex . Match ( t , "^(\\ d{4})/(1[012]|0?\\ d)/(30|31|[012]?\\ d) ([01]?\\ d|2[0123]):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
151- }
161+
162+ m = dateTimeRegex3 . Match ( t ) ; // 年月日 时分
163+ if ( m . Success == false ) { m = dateTimeRegex4 . Match ( t ) ; }
152164 if ( m . Success ) {
153165 MyDate date = new MyDate {
154166 Year = int . Parse ( m . Groups [ ( 1 ) ] . Value , cultureInfo ) ,
@@ -159,10 +171,8 @@ public static MyDate Parse(String txt)
159171 } ;
160172 return date ;
161173 }
162- m = Regex . Match ( t , "^(\\ d{4})-(1[012]|0?\\ d)-(30|31|[012]?\\ d)$" ) ;
163- if ( m . Success == false ) {
164- m = Regex . Match ( t , "^(\\ d{4})/(1[012]|0?\\ d)/(30|31|[012]?\\ d)$" ) ;
165- }
174+ m = dateRegex . Match ( t ) ; // 年月日
175+ if ( m . Success == false ) { m = dateRegex2 . Match ( t ) ; }
166176 if ( m . Success ) {
167177 MyDate date = new MyDate {
168178 Year = int . Parse ( m . Groups [ ( 1 ) ] . Value , cultureInfo ) ,
@@ -171,7 +181,8 @@ public static MyDate Parse(String txt)
171181 } ;
172182 return date ;
173183 }
174- m = Regex . Match ( t , "^(\\ d+) (2[0123]|[01]?\\ d):([012345]?\\ d):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
184+
185+ m = dayTimeRegex . Match ( t ) ; // 日 时分秒
175186 if ( m . Success ) {
176187 MyDate date = new MyDate {
177188 Day = int . Parse ( m . Groups [ ( 1 ) ] . Value , cultureInfo ) ,
@@ -181,7 +192,17 @@ public static MyDate Parse(String txt)
181192 } ;
182193 return date ;
183194 }
184- m = Regex . Match ( t , "^(2[0123]|[01]?\\ d):([012345]?\\ d):([012345]?\\ d)$" , RegexOptions . Compiled ) ;
195+ m = dayTimeRegex2 . Match ( t ) ; // 日 时分
196+ if ( m . Success ) {
197+ MyDate date = new MyDate {
198+ Day = int . Parse ( m . Groups [ ( 1 ) ] . Value , cultureInfo ) ,
199+ Hour = int . Parse ( m . Groups [ ( 2 ) ] . Value , cultureInfo ) ,
200+ Minute = int . Parse ( m . Groups [ ( 3 ) ] . Value , cultureInfo ) ,
201+ } ;
202+ return date ;
203+ }
204+
205+ m = timeRegex . Match ( t ) ; // 时分秒
185206 if ( m . Success ) {
186207 MyDate date = new MyDate {
187208 Hour = int . Parse ( m . Groups [ ( 1 ) ] . Value , cultureInfo ) ,
@@ -190,7 +211,7 @@ public static MyDate Parse(String txt)
190211 } ;
191212 return date ;
192213 }
193- m = Regex . Match ( t , "^(2[0123]|[01]? \\ d):([012345]? \\ d)$" , RegexOptions . Compiled ) ;
214+ m = timeRegex2 . Match ( t ) ; // 时分
194215 if ( m . Success ) {
195216 MyDate date = new MyDate {
196217 Hour = int . Parse ( m . Groups [ ( 1 ) ] . Value , cultureInfo ) ,
0 commit comments