【问题标题】:User friendly date input用户友好的日期输入
【发布时间】:2016-02-28 13:22:51
【问题描述】:

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

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'} 修改输出字符串。

【问题讨论】:

  • 前两个建议:1)不要在你的类中混合用于接口和数据表示的代码(因为它们是你将无法在没有终端/控制台的环境中重用它们,ej : web) 2) 完全使用 CultureInfo,除非它不符合您的需要。现在,我不确定这个问题,你想要可配置的日期格式吗?无论如何,“用户友好”是什么意思?编辑:我假设您已经测试了您提到的方法。 [您可以编辑您的问题以添加说明]
  • @Theraot - 我根本不明白你的第一点。这段代码有什么问题?我应该为某些数据创建另一个类吗?是的,我多次测试过这个程序。我的第二个问题是关于输出字符串。程序打印“学生出生于 yyyy-dd-mm”,我会将其切换为,例如,“学生出生于 dd/mm/yyyy”。
  • 没错。我的措辞可能太权威了。我这样说:有很多方法可以完成一项任务,有些更高效,有些更可重用,而且它们并不一致。您可能会越来越重视可重用性而不是效率。因此,我建议将 UI 与数据表示分开。当前的代码是否因为缺少这一点而行为不端?不,它应该可以正常工作。此外,此代码可能永远不会被迁移,因此它的可重用性可能并不重要。不过,在需要时让自己习惯是件好事。什么时候?当您想要相同的核心多个 UI 时。同时你可以忘记我说的话。
  • @Theraot 这显然是学生的作业,我敢肯定他永远不会重复使用它(除非他复读)
  • @Theraot,正如 Gusman 所说 - 这只是 edx 课程的作业,但感谢您的建议,我会尽量记住这一点:D

标签: c# date datetime


【解决方案1】:

听起来DateTime.TryParseExact 是个不错的方法。

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

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?这完全取决于您居住的地方以及您使用的文化设置。

我将如何操作输出格式日期?所以不会是 yyyy-dd-mm 但 dd/mm/yyyy 还是 dd.mm.yyyy?

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

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

【讨论】:

  • 是的,我知道这会引起误解,这就是为什么我在我的代码中添加了“(如 mm/dd/yyyy)”以及我可以添加到另一个“系统”中的相同内容。非常感谢您的回答:D
【解决方案2】:

这让用户以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"));
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多