C sharp

Moti Mashiah
Moti Mashiah used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
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 Developer

Author

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 Consultant
Top Expert 2016

Commented:
Considering that today we are Dec 2nd, what are you expecting to get as the output?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Moti Mashiah.NET Developer

Author

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 Consultant
Top Expert 2016

Commented:
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 Consultant
Top Expert 2016

Commented:
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 Developer

Author

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 Consultant
Top Expert 2016

Commented:
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 Developer

Author

Commented:
Thank you for your help.
It seems that you hard coded the days? I don't really get it.
Moti Mashiah.NET Developer

Author

Commented:
That should be a dynamic data.
Senior .Net Consultant
Top Expert 2016
Commented:
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!
Moti Mashiah.NET Developer

Author

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 Developer

Author

Commented:
Thanks for the great help.

Sorry if I wasn't clear enough.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial