Пример #1
0
        public static DateTime Subtract(DateTime dateTime, DateTimeSpan dateTimeSpan)
        {
            dateTime = dateTime.AddSeconds(dateTimeSpan.Seconds * -1);
            dateTime = dateTime.AddMinutes(dateTimeSpan.Minutes * -1);
            dateTime = dateTime.AddHours(dateTimeSpan.Hours * -1);
            dateTime = dateTime.AddDays(dateTimeSpan.Days * -1);
            dateTime = dateTime.AddDays(dateTimeSpan.Weeks * 7 * -1);
            dateTime = dateTime.AddMonths(dateTimeSpan.Months * -1);
            dateTime = dateTime.AddYears(dateTimeSpan.Years * -1);

            return(dateTime);
        }
Пример #2
0
        public static DateTime Add(DateTime dateTime, DateTimeSpan dateTimeSpan)
        {
            dateTime = dateTime.AddSeconds(dateTimeSpan.Seconds);
            dateTime = dateTime.AddMinutes(dateTimeSpan.Minutes);
            dateTime = dateTime.AddHours(dateTimeSpan.Hours);
            dateTime = dateTime.AddDays(dateTimeSpan.Days);
            dateTime = dateTime.AddDays(dateTimeSpan.Weeks * 7);
            dateTime = dateTime.AddMonths(dateTimeSpan.Months);
            dateTime = dateTime.AddYears(dateTimeSpan.Years);

            return(dateTime);
        }
Пример #3
0
        // Parsing

        public static DateTimeSpan Parse(string s)
        {
            s = s.Trim().ToLower();
            DateTimeSpan dateTimeSpan = new DateTimeSpan();

            Regex tokenRegex  = new Regex(@"\d+\s{0,1}\w+");
            Regex numberRegex = new Regex(@"\d{1,}");
            Regex wordRegex   = new Regex(@"[a-z]+");

            var matches = tokenRegex.Matches(s);

            List <Token> tokens = new List <Token>();

            for (int i = 0; i < matches.Count; i++)
            {
                tokens.Add(new Token(int.Parse(numberRegex.Match(matches[i].Value).Value), wordRegex.Match(matches[i].Value).Value));
            }

            for (int i = 0; i < tokens.Count; i++)
            {
                if (second.Contains(tokens[i].Word))
                {
                    dateTimeSpan.Seconds = tokens[i].Number;
                }
                else if (minute.Contains(tokens[i].Word))
                {
                    dateTimeSpan.Minutes = tokens[i].Number;
                }
                else if (hour.Contains(tokens[i].Word))
                {
                    dateTimeSpan.Hours = tokens[i].Number;
                }
                else if (day.Contains(tokens[i].Word))
                {
                    dateTimeSpan.Days = tokens[i].Number;
                }
                else if (week.Contains(tokens[i].Word))
                {
                    dateTimeSpan.Weeks = tokens[i].Number;
                }
                else if (month.Contains(tokens[i].Word))
                {
                    dateTimeSpan.Months = tokens[i].Number;
                }
                else if (year.Contains(tokens[i].Word))
                {
                    dateTimeSpan.Years = tokens[i].Number;
                }
            }

            return(dateTimeSpan);
        }
Пример #4
0
        public override bool Equals(object obj)
        {
            if ((obj == null) || !this.GetType().Equals(obj.GetType()))
            {
                return(false);
            }

            DateTimeSpan dateTimeSpan = (DateTimeSpan)obj;

            return(this.Days == dateTimeSpan.Days &&
                   this.Hours == dateTimeSpan.Hours &&
                   this.Minutes == dateTimeSpan.Minutes &&
                   this.Months == dateTimeSpan.Months &&
                   this.Seconds == dateTimeSpan.Seconds &&
                   this.Weeks == dateTimeSpan.Weeks &&
                   this.Years == dateTimeSpan.Years);
        }
Пример #5
0
 public static bool TryParse(string s, out DateTimeSpan dateTimeSpan)
 {
     dateTimeSpan = Parse(s);
     return(!dateTimeSpan.IsEmpty);
 }