C sharp

Hi Guys,
I'm using the DateTime class and I'm trying to return the last two week with this format:

Nov  1 - 15.

so, I'm doing something like:
List<DateTime> LastMonthFirst2Week = (from i in Enumerable.Range(0, 2) select DateTime.Today.AddDays(-(i * 12))).ToList();

 foreach (var t in LastMonthFirst2Week)
            {
                var tf = new Timeframe();
                tf.LastMonthFirst2Week = t.ToString("MMM d");
            }

Open in new window



by doing what I post above it gives me Nov 1 - so how do I get the end of it.? like the 15

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.

Éric MoreauSenior .Net ConsultantCommented:
Not sure to fully understand your expected output. How can the "last two week" can give you Nov 1 - 15? And you want a string as the output?
Moti Mashiah.NET DeveloperAuthor Commented:
This is exactly what I can't understand. :0

Do you have any idea how to output ToString("MMM d ------and the end day" );
Éric MoreauSenior .Net ConsultantCommented:
Considering that today we are Dec 2nd, what are you expecting to get as the output?
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Moti Mashiah.NET DeveloperAuthor Commented:
k, today is Dec 2 and I want to go 2 weeks back so the output I'm looking for to show is:
from the day Dec 2 to the previous two weeks. which actually should give me - Dec 2 - 19
Éric MoreauSenior .Net ConsultantCommented:
this will give you a string of the last 2 weeks:
DateTime d = DateTime.Today;
string s = d.AddDays(-14).ToString("MMM d") + " - " + d.ToString("MMM d");
MessageBox.Show(s);

Open in new window

Éric MoreauSenior .Net ConsultantCommented:
Dec 2 to 19  is not 2 weeks! and it is not previous, it is the future!

Consider this code:
DateTime d = DateTime.Today;
string prev = d.AddDays(-14).ToString("MMM d") + " - " + d.ToString("MMM d");
string next = d.ToString("MMM d") + " - " + d.AddDays(14).ToString("MMM d");
MessageBox.Show(prev + Environment.NewLine + next);

Open in new window

Moti Mashiah.NET DeveloperAuthor Commented:
Thank you for your solution it works, but its not exactly what I want  as I did what you did in a different way:

Here is an example:

List<DateTime> LastFirstWeek = (from i in Enumerable.Range(0, 2) select DateTime.Today.AddDays(-(i * 6))).ToList();
 foreach (var t in LastFirstWeek)
            {
                var tf = new Timeframe();
                tf.firstWeek = t.ToString("MMM d");
                lst.Add(tf);

            }

Open in new window



What I'm trying to do is to return month, then the date of today and the date of 2 weeks before:Capture.JPG
please look below:
Éric MoreauSenior .Net ConsultantCommented:
This method then?
private void button1_Click(object sender, EventArgs e)
{
    DateTime d = DateTime.Today;
    MessageBox.Show(GetSpecialDateRange(d));
}

private string GetSpecialDateRange(DateTime pDate)
{
    if (pDate.Day >= 1 && pDate.Day <= 15)
    {
        return pDate.ToString("MMM") + " 1 - 15";
    }
    else
    {
        return pDate.ToString("MMM") + " 16 - " + DateTime.DaysInMonth(pDate.Year, pDate.Month);
    }
}

Open in new window

Moti Mashiah.NET DeveloperAuthor Commented:
Thank you for your help.
It seems that you hard coded the days? I don't really get it.
Moti Mashiah.NET DeveloperAuthor Commented:
That should be a dynamic data.
Éric MoreauSenior .Net ConsultantCommented:
I don't get it. Your image is showing 1-15 and 16-31. Isn't it what you want? Pass any date to the method and it will return the range for that date.

Otherwise, I just don't get what you want. I have reread and I am just more confused!

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
Moti Mashiah.NET DeveloperAuthor Commented:
Actually you helped me a lot and thank you soo much. Now I understand this class much better.

I found exactlly what I need;

Here is my solution:

string datestr = dd.AddDays(-12).ToString("MMM d") + " " + (int)DateTime.Now.DayOfWeek;

Open in new window

Moti Mashiah.NET DeveloperAuthor Commented:
Thanks for the great help.

Sorry if I wasn't clear enough.
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
.NET Programming

From novice to tech pro — start learning today.