用户友好的日期输入

本文关键字:日期 输入 用户 | 更新日期: 2023-09-27 18:36:32

我必须编写代码程序,它会询问用户一些数据,然后用这些数据打印一些文本。下面只是一段代码(有效),还有其他变量。

class Student
{
    static public DateTime birthDay;
    public void GetStudentInformation()
    {
        Console.WriteLine("Enter student's birth date (as mm/dd/yyyy): ");
        birthDay = DateTime.Parse(Console.ReadLine());
    }
    public void PrintStudentData(ref DateTime birthDay)
    {
        Console.WriteLine("Student was born in {0}", birthDay.ToString("d"));
    }
}
class Program
{
    static void Main(string[] args)
    {
        Student newStudent = new Student();
        newStudent.GetStudentInformation();
        newStudent.PrintStudentData(ref Student.birthDay);
        Console.ReadKey()
    }
}

当我问生日时,我只需要日期,而不是确切的时间。有问题:

  1. 如何按用户更改输入日期,而不是 mm/dd/yyyy?
  2. 我将如何在输出格式日期进行操作?所以它不会是 yyyy-dd-mm,而是 dd/mm/yyyydd.mm.yyyy

我想补充一点,我真的是 C# 的初学者,并尝试了一些带有 CultureInfoParseExactTryParse 的代码,并使用 {0:'dd/mm/yyyy'} 修改输出字符串。

用户友好的日期输入

听起来DateTime.TryParseExact是一个很好的方法。

将日期和时间的指定字符串表示形式转换为其 使用指定格式的 DateTime 等效项,区域性特定 设置信息和样式的格式。字符串表示形式的格式 必须与指定的格式完全匹配。该方法返回一个值 指示转换是否成功。

DateTime birthDay;
if(DateTime.TryParseExact(Console.ReadLine(), "MM/dd/yyyy", 
                          CultureInfo.InvariantCulture, 
                          DateTimeStyles.None, out birthDay)
{
    // Your input string can (and will) be parsed with MM/dd/yyyy format.
}
else
{
    // Invalid format or value.
}

顺便说一句,我将您的mm更改为MM mm因为说明符是几分钟,但说明符是几个月MM

对于您的问题;

用户如何更改该输入日期将采用其他格式 月/日/年?

你不能这可能会产生很多模棱两可的情况,例如01/02/2016的格式是什么?是dd/MM/yyyy还是MM/dd/yyyy?这完全取决于您居住的地方以及您使用的区域设置。

我将如何在输出格式日期进行操作?所以不会 年-日-毫米,但

日/月/年或日.月.年?

对于输出,如果您指的是Console.WriteLine部分,则此 "d" 标准格式说明符使用运行此代码的ShortDatePattern CurrentCulture设置。这意味着输出格式取决于当前的区域性设置。如果此属性有dd/MM/yyyy,你会没事的。如果不是,则应使用自定义日期格式说明符对其进行格式化,例如;

Console.WriteLine("Student was born in {0}", 
                  birthDay.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));

允许用户以格式day/month/year输入并输出为dd.MM.yyyy

static void Main()
{
    Student newStudent = new Student();
    newStudent.GetStudentInformation();
    newStudent.PrintStudentData(ref Student.birthDay);
    Console.ReadKey();
    logEnd();
}
class Student
{
    static public DateTime birthDay;
    public void GetStudentInformation()
    {
        Console.WriteLine("Enter student's birth date as day/month/year");
        string[] formats = { "dd/MM/yyyy", "dd/M/yyyy", "d/M/yyyy", "d/MM/yyyy",
                "dd/MM/yy", "dd/M/yy", "d/M/yy", "d/MM/yy"};
        while (!DateTime.TryParseExact(Console.ReadLine(), formats,
             System.Globalization.CultureInfo.InvariantCulture,
             System.Globalization.DateTimeStyles.None,
             out birthDay))
        {
            Console.WriteLine("Your input is incorrect. Please input again.");
        }
        // User input correct, birthDay can now be used
    }
    public void PrintStudentData(ref DateTime birthDay)
    {
        Console.WriteLine("Student was born in {0}", birthDay.ToString("dd.MM.yyyy"));
    }
}