Exemple #1
0
        /// <summary>
        ///     Parses dates from a string. Results is set to Single if only one date was found, Two if two dates, Week if a week
        ///     was found ("Uke xx"), NoDate if no date was found.
        ///     Returns a list of dates. The list is empty if no dates were found. If a week was found, the first day of that week
        ///     is returned.
        /// </summary>
        /// <param name="date">The datestring to parse.</param>
        /// <param name="results">The results of the parsing.</param>
        /// <returns></returns>
        public DateTime[] ParseDate(string date, out DateParseResults results)
        {
            results = DateParseResults.NoDate;
            var dates = new List <DateTime>();

            var pattern = "(\\d)?\\d\\.(\\d)?\\d\\.";           // Regex pattern of the dates
            var match   = Regex.Match(date, pattern);           // Find first match

            if (match.Success)
            {
                // Since we found match, try to parse it to a date and add it to the dates list
                DateTime dt1;
                if (TryParse(match.Value, out dt1))
                {
                    results = DateParseResults.Single;
                    dates.Add(dt1);
                }

                // See if there is another match and if the datetext contains "og" (which means there are two dates)
                var nextMatch = match.NextMatch();
                if (date.ToLower().Contains("og") && nextMatch.Success)
                {
                    // Also parse this date and add it to the list
                    DateTime dt2;
                    if (TryParse(nextMatch.Value, out dt2))
                    {
                        results = DateParseResults.Two;
                        dates.Add(dt2);
                    }
                }
            }
            else
            {
                // Since we couldn't find a match, see if the datestring has "uke", which means it specifies a week
                if (date.ToLower().Contains("uke"))
                {
                    // Try to parse the weeknumber to a date (the first day of that week) and add it to the dates list
                    DateTime dt;
                    if (TryParseWeekToFirstDate(date, out dt))
                    {
                        results = DateParseResults.Week;
                        dates.Add(dt);
                    }
                    else
                    {
                        results = DateParseResults.NoDate;
                    }
                }
                else
                {
                    results = DateParseResults.NoDate;
                }
            }
            return(dates.ToArray());
        }
Exemple #2
0
        private const string ddMMyyyy_Format = "dd.MM.yyyy HH:mm:ss"; // Format for parsing date from eksamen/innlevering data

        /// <summary>
        ///     Parses the date and time specified (to the format dd.MM.yyyy HH:mm:ss). Results gives Single on succes, NoDate on
        ///     fail.
        ///     Checks if the two strings are null.
        /// </summary>
        public DateTime SimpleParse(string date, string time, out DateParseResults results)
        {
            results = DateParseResults.NoDate;
            DateTime dt;

            if (date != null && time != null &&
                DateTime.TryParseExact($"{date} {time}:00", ddMMyyyy_Format, CultureInfo.InvariantCulture, DateTimeStyles.None,
                                       out dt))
            {
                results = DateParseResults.Single;
                return(dt);
            }
            return(DateTime.MinValue);
        }