1

How do I write last 7 days except weekends.

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

for this code how can i pass weekends and this code comes from the 7th day today, I need the exact opposite .

if is it possible var day would be like =['25.05.2015','22.05.2015','21.05.2015','20.05.2015','19.05.2015','18.05.2015','15.05.2015'].

i will use this table for book record. 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)' }
                        });
Gktg
  • 49
  • 8
  • Where is the linq problem in some json data & parsing in a js function? Please modify your question and be clear about your problem – Razvan Dumitru May 26 '15 at 10:51

2 Answers2

1

Use AddBusinessDays:

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

with:

 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;
      }
 }

Read more here.

EDIT: duplicate of Adding Days to a Date but Excluding Weekends.

Community
  • 1
  • 1
L-Four
  • 13,345
  • 9
  • 65
  • 109
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());
            }

        }
    }
}
TWA
  • 12,756
  • 13
  • 56
  • 92