用户友好的日期输入 [英] User friendly date input

查看:73
本文介绍了用户友好的日期输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to code program, which will asks user about some data and then will print some text with this data. Below is just piece of code (which works), there are others variables too.

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()
    }
}

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

When I'm asking about birthday I need only date, not exactly time. There are questions:


  1. 如何按用户更改输入日期的格式不是 mm / dd / yyyy

  2. 如何处理输出格式日期?所以不是 yyyy-dd-mm ,而是 dd / mm / yyyy dd.mm.yyyy

  1. How to change that input date by user would be in other format than mm/dd/yyyy?
  2. How I would operate on output format date? So it wouldn't be yyyy-dd-mm but dd/mm/yyyy or dd.mm.yyyy?

我想补充一下,我真的是C#的初学者,并尝试了 CultureInfo 的一些代码, ParseExact TryParse 和使用 {0:'dd / mm / yyyy'修改输出字符串}

I want to add I'm really beginner in C# and tried some code with CultureInfo, ParseExact, TryParse, and modyfing output string with {0:'dd/mm/yyyy'}.

推荐答案

类似 DateTime.TryParseExact 是一个很好的方法。

Sounds like DateTime.TryParseExact is a good way to do it.


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

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.



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

By the way, I changed your mm to MM because mm specifier is for minutes but MM specifier is for months.

对于您的问题;


如何处理更改用户输入日期的格式不是
mm / dd / yyy?

How to change that input date by user would be in other format than mm/dd/yyy?

可以't 。这种可能会产生很多模棱两可的情况,例如 01/02/02 的格式是什么? code> dd / MM / yyyy MM / dd / yyyy ?这完全取决于您的住所以及所使用的文化设置

You can't. This might create a lot of ambiguous situations like what is the format of 01/02/2016? Is it dd/MM/yyyy or MM/dd/yyyy? This totally depends on where you live and which culture settings you use.


在输出格式日期我该如何操作?所以不是
yyyy-dd-mm,而是dd / mm / yyyy或dd.mm.yyyy?

How I would operate on output format date? So it wouldn't be yyyy-dd-mm but dd/mm/yyyy or dd.mm.yyyy?

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

With output, if you mean the Console.WriteLine part, this The "d" standard format specifier uses ShortDatePattern of your CurrentCulture settings where you run this code. That means the output format depends on the current culture settings. If this property has dd/MM/yyyy, you will be fine. If it is not, you should format it with customd date format specifiers like;

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

这篇关于用户友好的日期输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆