Asp.net c#

hi guys,
I'm trying to return dates from my server to a page and I'm using DateTime class.

The order I'm trying to return is:
For example "last month"  
1 jan - 7 jan - 8 jan - 14 jan - 15 jan - 21 jan - 22 jan - 28 jan

Any idea how to do it with datetime class.

Thanks.
LVL 1
Moti Mashiah.NET DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom GuethPresidentCommented:
If you are trying to pass multiple datetime objects, then you can use an array of dates or a List<datetime>.

Or am I misunderstanding the question?
Moti Mashiah.NET DeveloperAuthor Commented:
Thank you for your answer.

I do looping to get dates, but I'm not sure if I do it right can you review my code
This is just a sample to how I am trying to approach my goal.
int i = 1;
            List<string> lstLastmonth = new List<string>();
            DateTime[] lastMonth = Enumerable.Range(0, 28).Select(a => DateTime.Now.Date.AddDays(-a)).ToArray();
            foreach (var day in lastMonth)
            {
                if (i == 1)
                {
                    Console.WriteLine(day.Day.ToString());
                }
                
                if (i == 7)
                {
                    Console.WriteLine(day.Day.ToString());
                    Console.WriteLine(day.Day - 1);
                }
                  
                if (i == 14)
                    Console.WriteLine(day.Day.ToString());
                if (i == 21)
                    Console.WriteLine(day.Day.ToString());
                if (i == 28)
                    Console.WriteLine(day.Day.ToString());
                i++;
            }

Open in new window



My question is if there is any other way to do it and return last month in this order:

1 jan - 7 jan - 8 jan - 14 jan - 15 jan - 21 jan - 22 jan - 28 jan
it_saigeDeveloperCommented:
Exactly what I was going to propose tom.  If that is the format you are looking to display, then you simply need to get a collection of DateTime objects and use string.Join:
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q29071342
{
    class Program
    {
        static readonly List<DateTime> dates = (from i in Enumerable.Range(0, 8) select DateTime.Now.AddDays(-(i * 9))).ToList();
        static void Main(string[] args)
        {
            Console.WriteLine(string.Join(" - ", (from date in dates select date.ToString("d MMM"))));
            Console.ReadLine();
        }
    }
}

Open in new window

Which produces the following output -Capture.PNGOr using a loop to construct your output -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q29071342
{
    class Program
    {
        static readonly List<DateTime> dates = (from i in Enumerable.Range(0, 8) select DateTime.Now.AddDays(-(i * 9))).ToList();
        static void Main(string[] args)
        {
            var output = default(string);
            foreach(var date in dates)
            {
                if (output == default(string))
                {
                    output = date.ToString("d MMM");
                }
                else
                {
                    output += string.Format(" - {0}", date.ToString("d MMM"));
                }
            }
            Console.WriteLine(output);
            Console.ReadLine();
        }
    }
}

Open in new window

Which would produce the same output as above.

-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
Do you only want the dates you have defined?  What are you trying to achieve?

-saige-
Moti Mashiah.NET DeveloperAuthor Commented:
perfect thank you very much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.