I would like to get the 3 dates from the current date or if user enters a date like 16/07/2011
i would like to show the 3 previous dates for this like
15/07/2011,14/07/2011,13/07/2011
To display the previous day, use the AddDays() method and a value -1 to get the previous day.
<DateTime>. Date. AddDays(-1); This will strip off the time and give you midnight the previous day.
Extract the full weekday name using System; using System. Globalization; public class Example { public static void Main() { DateTime dateValue = new DateTime(2008, 6, 11); Console. WriteLine(dateValue. ToString("dddd", new CultureInfo("es-ES"))); } } // The example displays the following output: // miércoles.
Day); DateTime endDate = startDate. AddMonths(1). AddDays(-1);
Simple steps:
DateTime
. If you know the format to be used, I would suggest using DateTime.ParseExact
or DateTime.TryParseExact
.DateTime.AddDays(-1)
to get the previous date (either with different values from the original, or always -1 but from the "new" one each time)For example:
string text = "16/07/2011";
Culture culture = ...; // The appropriate culture to use. Depends on situation.
DateTime parsed;
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"));
}
}
else
{
// Handle bad input
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With