输入某年某月某日,判断这一天是这一年中的第几天。  昨天面试的时候一个题目,我没什么好的方法,用的switch case,虚心求简单高效的方法。

解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入日期:");
                string str = Console.ReadLine();
                DateTime dt = new DateTime();
                try
                {
                    dt = DateTime.Parse(str);
                    int year = dt.Year;
                    DateTime dt0 = new DateTime(year, 1, 1);
                    int days = (dt.Date - dt0.Date).Days + 1;
                    Console.WriteLine(string.Format("天数是:{0}。",days.ToString()));
                }
                catch
                {
                    Console.WriteLine("输入的日期格式不正确!");
                }
            }
        }

      

  2.   

    (dt.Date - dt0.Date).Days + 1;
     new B().
      

  3.   


                DateTime today = DateTime.Now;
                DateTime firstDay = new DateTime(today.Year, 1, 1);
                return rst = (today-firstDay ).Days;
      

  4.   

    select datediff(d,dateadd(yy,datediff(yy,0,@t),0),@t)
    (DateTime.Now-DateTime.Parse("2010-01-01")).Days