这种在c#中实现日期时间函数的方法有什么问题? [英] What is wrong with this way of implementing the datetime functions in c#

查看:78
本文介绍了这种在c#中实现日期时间函数的方法有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string input1 = "12/12/1991"

DateTime dt;

res = DateTime.TryParseExact(input1, "MM/dd/yyyy", null,System.Globalization.DateTimeStyles.None, out dt);

     if (res == true)
     {
             int year = DateTime.Now.Year - dt.Year;
             int month = DateTime.Now.Month - dt.Month;
             if (month < 0)
             {
               year = year - 1;
               month = month + 12;
             }
             if (year > 18)
             {
               Console.WriteLine("1");
             }
             else
             {
               Console.WriteLine("-1");
             }
      }



我没有获得上述代码的任何输出...帮助赞赏



[edit]已添加代码块 - OriginalGriff [/ edit]

格式化代码块 - Rohan Leuva [/ edit]


I am not getting any output for the above code... Help appreciated

[edit]Code block added - OriginalGriff[/edit]
[edit]Formatted code block- Rohan Leuva[/edit]

推荐答案

有趣 - 我愿意。

我得到1。



但是,看看这个:< a href =http://www.codeproject.com/Tips/569948/Working-with-Age-its-not-the-same-as-a-timespan>使用Age:它与TimeSpan不同! [ ^ ] - 我在前一段时间需要一个与年龄相关的课程时把它搞砸了,使用它应该比你的代码更清晰:

Interesting - I do.
I get "1".

However, have a look at this: Working with Age: it's not the same as a TimeSpan![^] - I knocked it up when I needed an age-related class a while ago, and it should be a little clearer to use than your code:
string input1 = "12/12/1991";
DateTime dt;
if (DateTime.TryParseExact(input1, "MM/dd/yyyy", null, System.Globalization.DateTimeStyles.None, out dt))
    {
    Age age = new Age(dt);
    if (age.Years >= 18)
        {
        Console.WriteLine("1");
        }
    else
        {
        Console.WriteLine("-1");
        }
    }


你想要完成什么?您对此代码的目标是什么?我必须在输入1的末尾添加一个';'并将res定义为bool。





使用下面的代码,我收到一个'1'的控制台输出。



What are you trying to accomplish? What is your intended goal for this code? I had to add a ';' to the end of input 1 and define res as a bool.


Using the below code, I received a console output of '1'.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input1 = "12/12/1991";

            DateTime dt;

            bool res = DateTime.TryParseExact(input1, "MM/dd/yyyy", null, System.Globalization.DateTimeStyles.None, out dt);

            if (res == true)
            {
                int year = DateTime.Now.Year - dt.Year;
                int month = DateTime.Now.Month - dt.Month;
                if (month < 0)
                {
                    year = year - 1;
                    month = month + 12;
                }
                if (year > 18)
                {
                    Console.WriteLine("1");
                }
                else
                {
                    Console.WriteLine("-1");
                }
            }
        }
    }
}


这篇关于这种在c#中实现日期时间函数的方法有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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