从输入中获取明天的日期

时间:2015-11-03 02:34:48

标签: c# .net

我需要我的代码才能通过用户输入获得第二天,但目前它只是从当前日期开始的第二天。我确实有一个已经工作的addDays,但我不确定如何通过任何一天进入第二天。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nextdate
{
    class Program
    {
        static void Main(string[] args) {
            Console.WriteLine("\nEnter Today's date to get next date");
            Console.Read(); // user inputs data
            Console.ReadLine();
            date currentdate = new date("");  
            Console.WriteLine("{0}", currentdate.getDay());
            Console.ReadLine();
        }

        public class date {
            public string day;    
            public int month;    
            public int year;

            public date(string day){  
                this.day = day;
            }

            public string getDay(){
                day = DateTime.Today.AddDays(1).ToString("dd");                 
                return day;                      
            }

            public int getMonth(){            
                month = DateTime.Today.Month;
                return month;
            }

            public int getYear(){           
                year = DateTime.Today.Year;
                return year;
            }
        }
    }
}

4 个答案:

答案 0 :(得分:1)

我认为你想要的只是明天的一天。如果今天是2015年12月25日(2015年12月25日)。你想看到结果为26.这是正确的。

更容易创建扩展方法而不是创建类。 但是,这是一个以简化版本使用您的类的示例。

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("\nEnter Today's date in dd/mm/yyyy to get next date ");
        var userDate = Console.ReadLine();
        MyDate currentdate = new MyDate(userDate);
        Console.WriteLine("{0}", currentdate.GetTomorrowDate());
        Console.ReadLine();
    }

    public class MyDate
    {
        public DateTime day;
        public MyDate(string inputDate)
        {
            this.day = DateTime.ParseExact(inputDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
        }
        public string GetTomorrowDate()
        {
            return day.AddDays(1).ToString("dd/MM/yyyy");
        }
    }
}

答案 1 :(得分:0)

不要使用" DateTime.Today"在你的getDay()中,而是将用户的输入数据(字符串)转换为(DateTime)数据类型,然后你就可以在转换后使用AddDays。

答案 2 :(得分:0)

您应该使用您的日期成员变量来形成用户输入日期,并从第二天返回。请参阅以下更新的代码:

public string getDay(){
int dateTimeDay;
var currentDate = DateTime.Today;
if int32.TryParse(this.day, out dateTimeDay) 
{
    retrun new DateTime(currentDate.Year, currentDate.Month, datetimeDay).AddDays(1).Day.ToString();
}
else
{
     retrun currentDate.AddDays(1).Day.ToString();
}
}

答案 3 :(得分:0)

如果我理解正确,您的要求是从用户处获取日期并显示第二天的日,月,年等。你可以这样做:

class Program
    {
        private static void Main()
    {
        //Format is given by 'dd-MM-yyyy'. You can change it to the one you want like 'dd/MM/yyyy' in below two lines and in 'GetTomorrowDateInStringFormat' method.
        //Then user will have to enter date in the given format
        Console.WriteLine("\nEnter Today's date to get next date (dd-MM-yyyy)");
        var date = DateTime.ParseExact(Console.ReadLine(), "dd-MM-yyyy", CultureInfo.InvariantCulture);
        var currentdate = new Date(date);
        Console.WriteLine("{0}", currentdate.GetDay());
        Console.WriteLine("{0}", currentdate.GetMonth());
        Console.WriteLine("{0}", currentdate.GetYear());
        Console.WriteLine("{0}", currentdate.GetTomorrowDateInStringFormat()); //Call to newly added method
        Console.ReadLine();
    }

    public class Date
    {
        public DateTime MyDate { get; set; }
        public string Day;
        public int Month;
        public int Year;

        public Date(DateTime date)
        {
            MyDate = date;
        }

        public string GetDay()
        {
            Day = MyDate.AddDays(1).ToString("dd");
            return Day;
        }

        public int GetMonth()
        {
            Month = MyDate.AddDays(1).Month;
            return Month;
        }

        public int GetYear()
        {
            Year = MyDate.AddDays(1).Year;
            return Year;
        }

        public string GetTomorrowDateInStringFormat() //Added new method to return tomorrow's date in string format
        {
            return MyDate.AddDays(1).ToString("dd-MM-yyyy");
        }
    }