【问题标题】:How to display the date in different formats in the console? [duplicate]如何在控制台中以不同格式显示日期? [复制]
【发布时间】:2019-12-10 08:29:29
【问题描述】:

如何在控制台中以不同格式显示日期?我需要制作一个可以以不同格式输出日期的类(dd/mm/yyyy、dd.mm.yyyy、dd.mmmm.yyyy)

 class FunctionWithDate
    {
    public int DifferentInDays(DateTime day1)
    {
        DateTime date2 = DateTime.Now; // текущая дата
        int ts = ((TimeSpan)(date2 - day1)).Days;
        /* Console.WriteLine(ts.Days);*/ // получаем разницу дней
        return ts;
    }

    public string GetdateByFormat(DateTime date, string format)
    {
        date = "dd/MM/yyyy";
        return format;
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Введите месяц, день  год");
            int day = Int32.Parse(Console.ReadLine());
            int month = Int32.Parse(Console.ReadLine());
            int year = Int32.Parse(Console.ReadLine());

            FunctionWithDate functionWithDate = new FunctionWithDate();
            DateTime enteredDate = new DateTime(day, month, year);

            int differentBeetweenDate = functionWithDate.DifferentInDays(enteredDate);
            Console.WriteLine(enteredDate);
            Console.WriteLine(differentBeetweenDate);

            ////////////////////////////////////////////////////

            string getDateByFormat = functionWithDate.GetdateByFormat(enteredDate,"dd/mm/yyyy");
            Console.WriteLine(getDateByFormat);
            Console.ReadKey();
        }
    }
}

}

【问题讨论】:

  • 欢迎来到 StackOverflow。 MSDN DateTIme ToString网页上有很多例子
  • 这段代码甚至无法编译。您不能将字符串分配给 DateTime 结构。但是,如果您查看上面提供的 MSDN 链接,您应该能够使用 format 参数来替换 ToString() 的参数并根据需要将日期转换为字符串。

标签: c# class date datetime


【解决方案1】:

可能你需要修改你的GetDateByFormat()方法:

public string GetdateByFormat(DateTime date, string format)
{
    return date.ToString(format);
}

那么,你可以像下面这样调用这个方法:

DateTime currentDate = DateTime.Now;
string dateAsString = GetDateByFormat(currentDate, "dd/MM/yyyy");
Console.WriteLine(dateAsString);

打印:10/12/2019

或喜欢:

DateTime currentDate = DateTime.Now;
string dateAsString = GetDateByFormat(currentDate, "MM yy, dd");
Console.WriteLine(dateAsString);

打印:12 19, 10

等等。

【讨论】:

    【解决方案2】:

    您可以在DateTime 对象上使用ToString() 来指定格式 - https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

    你需要类似 -

    DateTime enteredDate = new DateTime(day, month, year);
    
    Console.WriteLine(enteredDate.ToString("MMMM dd, yyyy"));
    Console.WriteLine(enteredDate.ToString("dd MM yyyy"));
    

    【讨论】:

      【解决方案3】:

      这是一个以多种格式显示日期的示例

      static void Main(string[] args)  
          {  
              // Get current DateTime. It can be any DateTime object in your code.  
              DateTime aDate = DateTime.Now;  
      
              // Format Datetime in different formats and display them  
              Console.WriteLine(aDate.ToString("MM/dd/yyyy"));  
              Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy"));  
              Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy"));  
              Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy"));  
              Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy"));  
              Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy"));  
              Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy HH:mm:ss"));  
              Console.WriteLine(aDate.ToString("MM/dd/yyyy HH:mm"));  
              Console.WriteLine(aDate.ToString("MM/dd/yyyy hh:mm tt"));  
              Console.WriteLine(aDate.ToString("MM/dd/yyyy H:mm"));  
              Console.WriteLine(aDate.ToString("MM/dd/yyyy h:mm tt"));  
              Console.WriteLine(aDate.ToString("MM/dd/yyyy HH:mm:ss"));  
              Console.WriteLine(aDate.ToString("MMMM dd"));  
              Console.WriteLine(aDate.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK"));  
              Console.WriteLine(aDate.ToString("ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’"));  
              Console.WriteLine(aDate.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss"));  
              Console.WriteLine(aDate.ToString("HH:mm"));  
              Console.WriteLine(aDate.ToString("hh:mm tt"));  
              Console.WriteLine(aDate.ToString("H:mm"));  
              Console.WriteLine(aDate.ToString("h:mm tt"));  
              Console.WriteLine(aDate.ToString("HH:mm:ss"));  
              Console.WriteLine(aDate.ToString("yyyy MMMM"));  
      
              Console.ReadKey();  
          }  
      

      【讨论】:

        【解决方案4】:

        您可以在DateTime 对象上使用ToString 方法。

        例如:DateTime.Now.ToString("dd.MM.yyyy")

        【讨论】:

          猜你喜欢
          • 2015-03-23
          • 2019-08-16
          • 2013-04-10
          • 2011-05-11
          • 2013-07-25
          • 1970-01-01
          • 1970-01-01
          • 2021-03-29
          • 2013-10-05
          相关资源
          最近更新 更多