C# - 用户友好的日期输入 [英] C# - user friendly date input

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

问题描述

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

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Globalization;

命名空间Uczymy3
{
class学生
{
static public DateTime birthDay;

public void GetStudentInformation()
{
Console.WriteLine(输入学生的出生日期(以mm / dd / yyyy为单位):);
birthDay = DateTime.Parse(Console.ReadLine());
}
public void PrintStudentData(ref DateTime birthDay)
{
Console.WriteLine(Student is born in {0},birthDay.ToString(d));
}
}
类程序
{
static void Main(string [] args)
{
学生newStudent = new Student() ;
newStudent.GetStudentInformation();
newStudent.PrintStudentData(ref Student.birthDay);
Console.ReadKey()
}
}
}

当我问生日时,我只需要约会,而不是时间。
有以下问题:


  1. 如何更改用户输入的日期格式与其他格式不同于 mm / dd / yyyy

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

我想添加我真的是C#中的初学者,并尝试使用 CultureInfo ParseExact TryParse ,并使用 {0:'dd / mm / yyyy'



感谢您的帮助。

解决方案

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


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




  DateTime birthDay ; 
if(DateTime.TryParseExact(Console.ReadLine(),MM / dd / yyyy,
CultureInfo.InvariantCulture,
DateTimeStyles.None,out birthDay)
{
//您的输入字符串可以(并且将被解析为MM / dd / yyyy格式
}
else
{
//格式或值无效
}

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



对于您的问题;


如何用户更改输入日期的格式不会超过
mm / dd / yyy?


您可以't 。这个可能会造成很多模糊的情况,比如$ code> 01/02/2016 的格式是什么? code> dd / MM / yyyy 或 MM / dd / yyyy ?这完全取决于你住的地方和你使用的文化设置。


我将如何操作输出格式日期?那么它不会是
yyyy-dd-mm但是dd / mm / yyyy或dd.mm.yyyy?


有了输出,如果你的意思是 Console.WriteLine part,这个 d标准格式说明符使用 ShortDatePattern 您的 CurrentCulture 运行此代码的设置。这意味着输出格式取决于当前的文化设置。如果这个属性有 dd / MM / yyyy ,你会没事的。如果不是,您应该使用自定义日期格式说明符进行格式化,如;

  Console.WriteLine(学生出生于{ 0},
birthDay.ToString(dd / MM / yyyy,CultureInfo.InvariantCulture));


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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace Uczymy3
{
    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. 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?

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'}.

Thanks for help.

解决方案

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

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.
}

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

For your questions;

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

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.

How I would operate on output format date? So it wouldn't be yyyy-dd-mm but dd/mm/yyyy or 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));

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

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