How to find out if the time part was present when analyzing DateTime

Let's say I have two lines containing date / time in an arbitrary custom format for an arbitrary culture:

string str1 = "6/19/2013"; string str2 = "6/19/2013 1 am"; 

I can analyze them by doing:

 DateTime dt1 = DateTime.Parse(str1); DateTime dt2 = DateTime.Parse(str2); 

But how do you know if a time part was present and analyzed in a DateTime object?

+4
source share
3 answers

What do you guys think of something like that?

 public static DateTime ParseDateTimeWithTimeDifferentiation(string str, out bool bOutTimePresent) { //Parse date/time from 'str' //'bOutTimePresent' = receives 'true' if time part was present DateTime dtRes; //Get formats for the current culture DateTimeFormatInfo dtfi = CultureInfo.CurrentUICulture.DateTimeFormat; DateTimeStyles dts = DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeLocal; //Get all formats string[] arrFmts = dtfi.GetAllDateTimePatterns(); foreach (string strFmt in arrFmts) { if (DateTime.TryParseExact(str, strFmt, CultureInfo.InvariantCulture, dts, out dtRes)) { //Parsed it! //These format codes come from here: // http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx bOutTimePresent = strFmt.IndexOfAny(new char[] { 'H', 'h', 'm', 's', 'f', 'F', 't', 'z' }) != -1; return dtRes; } } //As a fall-back, just parse it as-is dtRes = DateTime.Parse(str); //Assume it has time, as otherwise we'd catch the date-only above bOutTimePresent = true; return dtRes; } 
+1
source

You can use two separate calls for DateTime.ParseExact or DateTime.TryParseExact . It will be especially convenient if you know the date and time format. The code will look something like this:

 DateTime dateValue; var culture = new CultureInfo("en-US"); if (DateTime.TryParseExact(dateString, "M/d/yyyy H:mm:ss", culture, DateTimeStyles.None, out dateValue))) { //Using date and time. dateValue var is set. } else if (DateTime.TryParseExact(dateString, "M/d/yyyy", culture, DateTimeStyles.None, out dateValue))) { //Using just date. dateValue var is set. } 

If you cannot predict the exact format of the date / time string, you can either list a bunch of possible formats, or use regular expressions to try to extract the time part. Read more about custom date and time formats here . There are also some standard date and time formats .

0
source

If no time is specified in the line that was analyzed, the TimeOfDay property of the DateTime object will be set at midnight (00:00:00). Then you can check if this case has something like this:

 if (dt1.TimeOfDay.Equals(new TimeSpan(0,0,0))) { //do something } else { //do something else } 

EDIT: Another approach might be to separate the date and time strings of a string. This assumes that some type of numeric date format is passed using dashes, commas, or anything other than spaces.

 string[] dateString = str1.Split(' '); string[] date2String = str2.Split(' '); 

You will now have a string array that you can easily use to check for special values. dateString[0] , for example, should contain all of your date. dateString[1] and onwards will have any time formats and can be recombined and analyzed into a TimeSpan object. Obviously, if there is only one entity in the array, they have not entered at any time.

-1
source

Source: https://habr.com/ru/post/1487188/


All Articles