| 
									
										
										
										
											2023-03-29 17:56:42 +02:00
										 |  |  | import { Validator } from "../Validator" | 
					
						
							| 
									
										
										
										
											2023-03-29 17:21:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class DateValidator extends Validator { | 
					
						
							|  |  |  |     constructor() { | 
					
						
							|  |  |  |         super("date", "A date with date picker") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     isValid(str: string): boolean { | 
					
						
							|  |  |  |         return !isNaN(new Date(str).getTime()) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     reformat(str: string) { | 
					
						
							| 
									
										
										
										
											2023-04-24 03:36:02 +02:00
										 |  |  |         console.log("Reformatting", str) | 
					
						
							|  |  |  |         if (!this.isValid(str)) { | 
					
						
							|  |  |  |             // The date is invalid - we return the string as is
 | 
					
						
							|  |  |  |             return str | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-29 17:21:20 +02:00
										 |  |  |         const d = new Date(str) | 
					
						
							|  |  |  |         let month = "" + (d.getMonth() + 1) | 
					
						
							|  |  |  |         let day = "" + d.getDate() | 
					
						
							|  |  |  |         const year = d.getFullYear() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (month.length < 2) month = "0" + month | 
					
						
							|  |  |  |         if (day.length < 2) day = "0" + day | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return [year, month, day].join("-") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |