I get date and time from CSV file
The received Date format is YYYYMMDD (string) (there is no ":" ,"-","/" to separate Year month and date). The received time format is HH:MM (24 Hour clock).
I need to check both options so that (example) (i) 000011990 can be invalidated on date (ii) 77:90 can be invalid for time.
A regular expression is the right candidate for this (or) is there any other way to achieve this?
You are looking for DateTime.TryParseExact:
DateTime.TryParseExact
string source = ...; DateTime date; if (!DateTime.TryParseExact(source, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) { //Error! }
You can use the same code to check the time with a format string "HH:mm".
"HH:mm"
The easiest solution is to use
DateTime output; if(!DateTime.TryParse(yourstring, out output)) { // string is not a valid DateTime format }
DateTime.TryParse DateTime, , - , false, DateTime.
DateTime.TryParse
false
I think the best way is to use the date format class built into C #: DateTime.parse
You can use one of the TryParsestructure methods DateTime. They will return false if they are not parsed.
TryParse
DateTime
In another option, it uses methods ParseExact, but for them you need to specify a format provider.
ParseExact
Source: https://habr.com/ru/post/1740657/More articles:Is there an ActiveRecord equivalent for using the nested subquery ie ... where is NOT IN (select ...)? - ruby-on-railsA bunch of Java continues to shrink! What happens on this heap size graph? - javaJqGrid not working in ASP.NET MVC2 - jsonКакие классы Qt используют диск напрямую? - multithreadingQuestion about compiling temporary C ++ polymorphism? - c ++double fork using vfork - c ++setuid () before calling execv () in vfork () / clone () - cProcessing: Why does setup () work several times? - processingMethod for determining the width of an entire image in PGF (latex) - macrosReading a line in an assembly TASM x86 - assemblyAll Articles