Hi, again about DateTime method I get to some conclusionns and build a class of my problem that solve the last expired day of some product I get a day od today and number of month when product can used without any problem and the day when product was made
and compare these two days...
i want to here your opinion about this clas and usinf the Datetime method
Thanks Sergey

class Date
    {
        //data memebers
        private DateTime today = DateTime.Now;  // variable for todays data
        private DateTime published=new DateTime(1900,1,1);//variable for date when product made
        private int month;  // the number of month that product can used
        private DateTime ExDay;// the day when product is expired
        //constructors
        public Date(DateTime today, DateTime published, int month)
        {
            this.today = today;
            this.published = published;
            this.month = month;

        }

        //properties
        public DateTime Publish
        {
            get { return published; }
            set
            {
                Console.Write(" enter a date when a product was made in format(yy/mm/dd) :");
                published = value;
            }

        }
        
        public int MonthEx
        {
            get { return month; }
            set
            {
                Console.Write(" Enter a number of month user can used a product :");
                do
                {
                    month = value;
                    if (month <= 0) Console.WriteLine(" Wrong number Please Re-Enter!!");
                } while (month <= 0);
            }
        }
        //Methods
        //function that solve the expired day
        private DateTime Expired()
        {
            ExDay=published.AddDays(month * 30);
            return ExDay;
        }
        //function that solve if it already over the last expired day or not
        public void  Condition()
        {
            int cond = DateTime.Compare(today, ExDay);
            if (cond >= 0) Console.WriteLine(" You can use the product !!!");
            Console.WriteLine(" You can't use the product!!!");
        }

            

        // bool todayEqualsToday = DateTime.Equals(today1, today2);
           // Console.WriteLine(todayEqualsToday);
           // // todayEqualsTomorrow gets false.
           // bool todayEqualsTomorrow = DateTime.Equals(today1, tomorrow);
           // Console.WriteLine(todayEqualsTomorrow);






            //    DateTime today = DateTime.Now;
            //DateTime answer = today.AddDays(1);
            //Console.WriteLine("{0}", answer);


           //  DateTime today1 = 
           //         new System.DateTime(DateTime.Today.Ticks);
           // DateTime today2 = 
           //         new DateTime(DateTime.Today.Ticks);
           //DateTime tomorrow = 
           //         new System.DateTime(
           //                     DateTime.Today.AddDays(1).Ticks);

           // // todayEqualsToday gets true.
           // bool todayEqualsToday = DateTime.Equals(today1, today2);
           // Console.WriteLine(todayEqualsToday);
           // // todayEqualsTomorrow gets false.
           // bool todayEqualsTomorrow = DateTime.Equals(today1, tomorrow);
           // Console.WriteLine(todayEqualsTomorrow);




        
    }

Dani AI

Generated

posted a useful starting point and rightly called out readability. The core recommendations below focus on correctness and separation of concerns so the expiry calculation is reliable and the class is easy to test and reuse.

Keep I/O out of properties and constructors. Do validation where the object is built, not inside a setter that prompts the user. Avoid sentinel dates; prefer throwing an ArgumentException or using a nullable DateTime if “no date” is meaningful. Use months-aware arithmetic instead of approximating months as a fixed number of days to avoid errors across month lengths and leap years.

A compact, safer design pattern:

public class ProductExpiry
{
    public DateTime ManufactureDate { get; }
    public int ValidMonths { get; }

    public ProductExpiry(DateTime manufactureDate, int validMonths)
    {
        if (validMonths <= 0) throw new ArgumentOutOfRangeException(nameof(validMonths));
        ManufactureDate = manufactureDate;
        ValidMonths = validMonths;
    }

    public DateTime ExpiryDate() => ManufactureDate.AddMonths(ValidMonths);

    public bool IsExpiredOn(DateTime referenceDate) => referenceDate > ExpiryDate();
}

This keeps responsibilities clear: construction/validation, expiry calculation, and a single comparison method. Decide whether expiry is inclusive (<=) or exclusive (>) and document it.

Extra tips: validate and parse user input using TryParseExact to avoid culture-dependent errors. If time zones matter, prefer DateTimeOffset. For reference on month-aware addition use the official docs for AddMonths: DateTime.AddMonths and for parsing use DateTime.TryParseExact.

Recommended Answers

All 2 Replies

As it wasnt set in coded blocks its harder to read, it also has a lot of commented code, so theres little to comment on.. other than your "console.write" lines dont actually "get" the value.. or display it.. they'll appear as random unwanted lines.

What did you want the comment on? Your english? Your code? are we to guess what you're doing with it?

As it wasnt set in coded blocks its harder to read, it also has a lot of commented code, so theres little to comment on.. other than your "console.write" lines dont actually "get" the value.. or display it.. they'll appear as random unwanted lines.

What did you want the comment on? Your english? Your code? are we to guess what you're doing with it?

my english I know that not so perfect, I already wrote a lot of comments that you already know what i want to do, All i asked is this code wroten in right way or not and if you can suggest other shorter and more understood way

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.