除了周末到天之外,如何写最近7天

时间:2015-05-26 10:43:35

标签: c#

除了周末外,我怎么写最近7天。

for (var i = -7;i == 0 ;i++)
    { DateTime date = new DateTime();
    var Days = date.AddDays(i).ToString();
    }  

对于这段代码我怎么能通过周末,这段代码来自今天的第7天,我需要完全相反。

如果有可能var var就像= [''25.05.2015','22.05.2015','21.05.2015','20.05.2015','19.05.2015','18.05.2015', '15 .05.2015' ]。

我将使用此表进行书籍记录。 enter image description here

function drawVisitorsChart() {


                        var data = new google.visualization.DataTable();
                        var raw_data = [['Book', 50, 73, 104, 129, 146, 176, 139],
                                        ['Periodical', 82, 77, 98, 94, 105, 81, 104],
                                        ['Map', 50, 39, 39, 41, 47, 49, 150]];

                        var Days= ['x', 'x', 'x', 'x', 'x', 'x', 'x'];

                        data.addColumn('string', 'Month');
                        for (var i = 0; i < raw_data.length; ++i) {
                            data.addColumn('number', raw_data[i][0]);
                        }

                        data.addRows(Days.length);

                        for (var j = 0; j < Days.length; ++j) {
                            data.setValue(j, 0, months[j]);
                        }
                        for (var i = 0; i < raw_data.length; ++i) {
                            for (var j = 1; j < raw_data[i].length; ++j) {
                                data.setValue(j - 1, i + 1, raw_data[i][j]);
                            }
                        }


                        var div = $('#daily_div');
                        new google.visualization.ColumnChart(div.get(0)).draw(data, {
                            title: 'Daily Record',
                            width: div.width(),
                            height: 330,
                            legend: 'right',
                            yAxis: { title: '(thousands)' }
                        });

2 个答案:

答案 0 :(得分:1)

使用AddBusinessDays

for (var i = -7;i == 0 ;i++)
{ 
   DateTime date = new DateTime();
   var Days = date.AddBusinessDays(i).ToString();
}  

使用:

 public static class DateTimeExtensions
 {
     public static DateTime AddBusinessDays(this DateTime date, int days)
     {
         double sign = Convert.ToDouble(Math.Sign(days));
         int unsignedDays = Math.Sign(days) * days;
         for (int i = 0; i < unsignedDays; i++)
         {
             do
             {
                 date = date.AddDays(sign);
             }
             while (date.DayOfWeek == DayOfWeek.Saturday || 
                date.DayOfWeek == DayOfWeek.Sunday);
         }
         return date;
      }
 }

了解更多here

编辑:Adding Days to a Date but Excluding Weekends的副本。

答案 1 :(得分:0)

Screenshot

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

namespace SkipWeekends
{
    class Program
    {
        static List<DateTime> GetBusinessDays(DateTime startDate, int numDays)
        {
            var dates = new List<DateTime>();

            var step = (numDays < 0) ? -1 : 1;
            var date = startDate;
            var absNumDays = Math.Abs(numDays);

            while(dates.Count() < absNumDays)
            {
                date = date.AddDays(step);

                if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                    continue;

                dates.Add(date);
            }

            return dates;
        }

        static void Main(string[] args)
        {
            var dates = new List<DateTime>();
            var start = DateTime.Now;

            dates = GetBusinessDays(start, 14);

            Console.WriteLine("14 Business Days in the Future:\n");

            foreach(var date in dates)
            {
                Console.WriteLine(date.ToString());
            }

            dates = GetBusinessDays(start, -14);

            Console.WriteLine("\n\n14 Business Days in the Past:\n");

            foreach (var date in dates)
            {
                Console.WriteLine(date.ToString());
            }

        }
    }
}