ArshamCoder
ArshamCoder

Reputation: 119

How can i use Date-time operator?

at VS I press the F12 key on DateTime and at the end of the DateTime Struct I see these:

        public static DateTime operator +(DateTime d, TimeSpan t);
        public static TimeSpan operator -(DateTime d1, DateTime d2);              
        public static DateTime operator -(DateTime d, TimeSpan t);
        public static bool operator ==(DateTime d1, DateTime d2);
        public static bool operator !=(DateTime d1, DateTime d2);       
        public static bool operator <(DateTime t1, DateTime t2);
        public static bool operator >(DateTime t1, DateTime t2);
        public static bool operator <=(DateTime t1, DateTime t2);
        public static bool operator >=(DateTime t1, DateTime t2);

always I compare DateTime in this way. for example:

            DateTime date1=DateTime.Now;
            DateTime date2=DateTime.Now.AddDays(-2);

            if (date1 > date2)
            {
                // date1 greater than date2
            }
            else
            {  
                // date2 greater than date1
            }

but how can I use those methods? can somebody give an example of that? thanks in advance

Upvotes: 0

Views: 457

Answers (1)

Lewitas
Lewitas

Reputation: 66

In your example you are using method operator >. This methods are definied operators for class DateTime. You can implement operators for your own class to save time on coding. Here you have documentation about operator overloading. I hope it helps you undestand it more.

Upvotes: 3

Related Questions