File tree Expand file tree Collapse file tree 2 files changed +12
-6
lines changed Expand file tree Collapse file tree 2 files changed +12
-6
lines changed Original file line number Diff line number Diff line change @@ -166,12 +166,12 @@ export function maxValue() {
166166}
167167
168168export function parseRaw ( input : string ) {
169- if ( input === null ) {
170- throw new Error ( "Value cannot be null when parsing DateTime" ) ;
169+ function fail ( ) {
170+ throw new Error ( `The string is not a valid Date: ${ input } ` ) ;
171171 }
172172
173- if ( input . trim ( ) === "" ) {
174- throw new Error ( "An empty string is not recognized as a valid DateTime" ) ;
173+ if ( input === null || input . trim ( ) === "" ) {
174+ fail ( ) ;
175175 }
176176
177177 let date = new Date ( input ) ;
@@ -217,11 +217,12 @@ export function parseRaw(input: string) {
217217 // correct for daylight savings time
218218 date = new Date ( date . getTime ( ) + ( date . getTimezoneOffset ( ) - baseDate . getTimezoneOffset ( ) ) * 60000 ) ;
219219 } else {
220- throw new Error ( "The string is not a valid Date." ) ;
220+ fail ( ) ;
221221 }
222222
223+ // Check again the date is valid after transformations, see #2229
223224 if ( isNaN ( date . getTime ( ) ) ) {
224- throw new Error ( "The string is not a valid Date." ) ;
225+ fail ( ) ;
225226 }
226227 }
227228
Original file line number Diff line number Diff line change @@ -251,6 +251,11 @@ let tests =
251251 f " 9/10/2014 1:50:34 PM" |> equal true
252252 f " 1:50:34" |> equal true
253253
254+ testCase " Parsing doesn't succeed for invalid dates" <| fun () ->
255+ let invalidAmericanDate = " 13/1/2020"
256+ let r , _date = DateTime.TryParse invalidAmericanDate
257+ r |> equal false
258+
254259 testCase " DateTime.Today works" <| fun () ->
255260 let d = DateTime.Today
256261 equal 0 d.Hour
You can’t perform that action at this time.
0 commit comments