Hi,
How can i get the previous three dates from the current date. for example the current date is 16/07/2011. so can i get the previous dates(15/07/2011,14/07/2011,13/07/2011).note that month can also be changed. like if date is 01/07/2011 and i want to get the previous three dates so in this case not only day but also month will be changed. so please tell me how to do this using asp.net with c#
Loading
Dorababu MekaPosted Jul 16, 2011, 3:06 AM
DateTime dt = DateTime.Now;
DateTime dt1 = dt.AddDays(-1); // WIll get previous date
DateTime dt2 = dt1.AddDays(-1); // Will get the 2nd previous date
Like this use as per your need
If you are taking as string use as per below
string str = "16/07/2011";
DateTime dt1 = DateTime.ParseExact(str, "dd/MM/yyyy", null);
dt1 = dt1.AddDays(-1);
DateTime dt2 = dt1.AddDays(-1);
You can loop too i tried in windows forms
string text = "16/07/2011";
DateTime parsed;
CultureInfo culture = new CultureInfo("en-US");
if (DateTime.TryParseExact(text, "dd/MM/yyyy", culture, DateTimeStyles.None, out parsed))
{
for (int i = 1; i <= 3; i++)
{
Console.WriteLine(parsed.AddDays(-i).ToString("dd/MM/yyyy"));
if (i == 1)
{
textBox1.Text = parsed.AddDays(-i).ToString();
}
if (i == 2)
{
textBox2.Text = parsed.AddDays(-i).ToString();
}
if (i == 3)
{
textBox3.Text = parsed.AddDays(-i).ToString();
}
}
}