Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 除了未来,如何设定日期?_C# - Fatal编程技术网

C# 除了未来,如何设定日期?

C# 除了未来,如何设定日期?,c#,C#,我正在写一个程序,用户必须输入一个日期。我的问题是: 如何将日期设置为今天和过去,而不是未来 如何将日期设置为非US格式,即dd/mm/yyyy,以便编译器将中间值读取为月 这就是我的代码的样子: static DateTime date; 像这样的方法 public static void EnterDates() { for (int i = 0; i < days; i++) { Console.Write("Enter the date (dd/

我正在写一个程序,用户必须输入一个日期。我的问题是:

  • 如何将日期设置为今天和过去,而不是未来
  • 如何将日期设置为非US格式,即dd/mm/yyyy,以便编译器将中间值读取为月
  • 这就是我的代码的样子:

     static DateTime date;
    
    像这样的方法

    public static void EnterDates()
    {
        for (int i = 0; i < days; i++)
        {
            Console.Write("Enter the date (dd/mm/yyyy): ");
            date = DateTime.Parse(Console.ReadLine());
            centers[k].dates[i] = date;
            Console.WriteLine("Day " + centers[k].dates[i]);
            Console.Write("Number of movie screenings: ");
            movieScreen = Convert.ToInt32(Console.ReadLine());
            centers[k].movieScreen[i] = movieScreen;
            Console.Write("Total number of customers: ");
            customers = Convert.ToInt32(Console.ReadLine());
            centers[k].customers[i] = customers;
            centers[k].revenue[i] = movieScreen * customers * (centers[k].Price * 1.13);
            centers[k].totalRevenue += centers[k].revenue[i];
    
    public静态无效EnterDates()
    {
    对于(int i=0;i
    如何将日期设置为今天和过去,而不是未来

    根据您当前拥有的数据,您可以在用户输入
    日期后添加验证,并将其与
    日期时间进行比较。现在

    if(date < DateTime.Now)
       //valid
    else
       //invalid
    
    date = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy", CultureInfo.InvariantCulture)