本文介绍了出生后1000天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近参加了一个有关C#的课程,我真的很喜欢它,但是我进行了一次练习,其中我必须写一个日期,并且应该在1000天后给我一个日期。在研究了DateTime类型后,我编写了某种代码,但它始终为我提供1001天而不是1000天之后的日期。另外,我也不想花时间在最终结果上,所以我真的需要帮助...。如果我的话题很愚蠢,我想道歉...我只是在这里打了个字,这是我的第一篇文章。
这是代码:

I recently joined a course about C# and I'm really enjoying it but I've got an exercise in which I have to write a date and it should give me back a date after 1000 days. I write some kind of code after a research about the DateTime type but it always gives me a date after 1001 days and not 1000 days. Also I don't want to have time on my final result so I really need help.... I want to apologies if my topic is dumb af... I just made an account here and that's my first post.So here's the code:

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

namespace _1000DaysAfterBirthV2
{
    class Program
    {
        static void Main(string[] args)
        {
            string birthDate = Console.ReadLine();
            DateTime d = Convert.ToDateTime(birthDate).Date;
            DateTime.ParseExact(birthDate, "dd-MM-yyyy", null);
            DateTime birthday = DateTime.Parse(birthDate);
            DateTime after = birthday.AddDays(1000);
            Console.WriteLine(after);
        }
    }
}


推荐答案

要显示没有时间的日期,请查看方法。

As for showing the date without the time, look into the .ToString() method.

after.ToString( "MM/dd/yyyy" );

这与您的解析类似,MM表示月,MM表示分钟。不同的代表性部分。在输出中选择所需的任何部分。 ToString()的类似应用程序也用于数字字段,例如int,decimal,float以显示前导/尾随零,十进制精度等。所有对象都有默认的 ToString()方法,您甚至可以定义自己的方法

It is similar to your parsing, MM for month vs mm for minutes. The different representative parts. Pick whatever parts you want in your output. Similar applications of ToString() are used for numeric fields too, such as int, decimal, float to show leading/trailing zeros, decimal precision, etc. All objects have a default "ToString()" method which you can even define your own on your own custom classes when you get that far down...

澄清

调用它就像上面是根据您对 After DateTime变量类型的引用而得出的。 ToString()返回具有特定格式的字符串。
在调用WriteLine()时,需要一个字符串。由于您要传递之后的DateTime,因此该方法知道需要调用各自的.ToString()并隐含默认输出。上面的所有示例都明确地告诉WriteLine在之后打印日期字段,但使用包括的SPECIFIC格式。如果您希望以其他格式显示日期,则可以

Calling it is exactly as I have it above based on your reference to the "after" DateTime variable type. ToString() returns a string with a specific format.When you are calling WriteLine(), that is expecting a string. Since you are passing in your "after" DateTime, the method knows it needs to call the respective .ToString() and implies a default output. All the above sample does is explicitly tells WriteLine to print the date field "after", but using the SPECIFIC format included. If you wanted the date shown in other formats you could do

after.ToString("yyyy-MM-dd") -- year first, then month and day.
after.ToString("dd-MM-yyyy") -- day first, then month, then year
after.ToString("MM-yyyy" )   -- to only show 2-digit month and 4-digit year

这篇关于出生后1000天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:04