Link to home
Create AccountLog in
Avatar of Moti Mashiah
Moti MashiahFlag for Canada

asked on

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.
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

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?
Avatar of Moti Mashiah

ASKER

This is exactly what I can't understand. :0

Do you have any idea how to output ToString("MMM d ------and the end day" );
Considering that today we are Dec 2nd, what are you expecting to get as the output?
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
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

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

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:User generated image
please look below:
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

Thank you for your help.
It seems that you hard coded the days? I don't really get it.
That should be a dynamic data.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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

Thanks for the great help.

Sorry if I wasn't clear enough.