什么是最优化以下中的版本,而无需使用NET的时间跨度或日期时间的C#。我将如何NUnit的测试吗?

TimeSpan ts = Date1 - Date2;
int numberOfDays = ts.Days;
有帮助吗?

解决方案

您可以将日期转换为蜱,。减去,然后再转换蜱回天。虽然究竟为什么你不能使用时间跨度?很可能这样做。

像这样:

      DateTime a = DateTime.Now;
      DateTime b = a.AddDays(2);

      // ticks are in hns
      long ticks = b.Ticks - a.Ticks;
      long seconds = ticks / 10000000;
      long minutes = seconds / 60;
      long hours = minutes / 60;
      long days = hours / 24;

我回来 “2” 的日子。

其他提示

日期1和日期2是什么类型?在您的exameple它看起来是日期时间。您希望您在什么变量日期,如果不是的DateTime?你总是可以有你的日期1和date2的字符串,并用字符串()发挥得年,月,日,但是这将是一个真正的痛苦的工作。

在优化的方式做你的问题:

TimeSpan ts = Date1 - Date2;
int numberOfDays = ts.Days;

DateTime Date1 = new DateTime(2008,12,01);
DateTime Date2 = new DateTime(2008,12,25);
int numberOfDayBetweenDateThatShouldBe = 25;
Assert.IsTrue((Date2-Date1).TotalDays == numberOfDayBetweenDateThatShouldBe);

感谢,这是家庭作业(asp.net)

的任务是写在c#即需要2个日期作为输入来自用户的asp.net应用程序,工程出它们之间的天数,并显示该号码回给用户。这是不应该使用任何内置的.NET Framework日期时间或时间跨度类。

唉我在生产实力最优雅的执行

private int CalculateDays(DateTime start, DateTime end )
{
   DateTime origin = new DateTime(); 
   return (end - origin).Days - (start - origin).Days;
}

protected void Button1_Click(object sender, EventArgs e)
{
    // Parse dates for correctness and range errors, warn as necessary
    DateTime start;
    DateTime end;

    // rough error implement error handling implementation   
    string errors = string.Empty;

         if(!DateTime.TryParse(txt_start_date.Text.Trim(), out start))   errors+="Start date was incorrect";
    else if(!DateTime.TryParse(txt_end_date.Text.Trim(), out end))   errors+= (errors.Length>0? errors+= "\n":"") + "End date was incorrect" ; 
    else if ((end.Day - start.Day) <= 0) errors+= (errors.Length>0? errors+= "\n":"" ) + "End date must be greater than the Start date" ;         //CultureInfo.InvariantCulture
    else 
    {
        Response.Write(CalculateDays(start, end)); 
    }
    Debug.Assert(errors.Length <= 0, errors); 
}

原因是我无法看到如何做到这一点没有至少接触DateTime.Days。


修改 还好又想起了,有点猴子的工作,但是,没有使用日期时间或时间跨度的任何地方

public static class PrimitiveCal
{
    // incremental total days a normal year 
    private static int[] DaysInYr = new int[] {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

    // incremental total days in a leap year 
    private static int[] DaysInLeapYr = new int[] { 0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };


    public static long GetDays(int[] dt_start, int[] dt_end)
    {

        int day_diff = dt_end[0] - dt_start[0];
        int s_mth = dt_start[1];
        int e_mth = dt_end[1];
        int s_year = dt_start[2];
        int e_year = dt_end[2];
        int yr_diff = e_year - s_year;

        return day_diff + (yr_diff > 0 ?
                         // use months as boundaries, cater for leap years
                         (YrType(e_year)[e_mth - 1] +
                         (YrType(s_year)[YrType(s_year).Length - 1] - YrType(s_year)[s_mth - 1])) +
                         (yr_diff == 1 ? 0 : AddMiddleYears(s_year, e_year))

                         // get month sums in same year 
                         : YrType(e_year)[e_mth - 1] - YrType(e_year)[s_mth - 1]);
    }

    private static int AddMiddleYears(int s_year, int e_year)
    {
        int total_days = 0;
        for (int i = s_year + 1; i <= e_year - 1; i++) total_days += YrType(i)[YrType(i).Length - 1];
        return total_days;
    }

    private static int[] YrType(int year)
    {
        return (year % 4  == 0 &&  year%100!=0) || year % 400 ==0 ? DaysInLeapYr : DaysInYr;
    }
}

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top