如何在控制台中以不同的格式显示日期?我需要制作一个可以以不同格式(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();
}
}
}
}
答案 0 :(得分:0)
您可以在ToString()
个对象上使用DateTime
来指定格式-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"));
答案 1 :(得分:0)
您可以在ToString
对象上使用DateTime
方法。
例如:DateTime.Now.ToString("dd.MM.yyyy")
答案 2 :(得分:0)
这是一个以多种格式显示日期的示例
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();
}
答案 3 :(得分:0)
可能您需要修改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
等