This question already has answers here:
Compute the DateTime of an upcoming weekday
(11 answers)
Closed 9 years ago.
I need to retrieve the date, month, and year of the last Tuesday relative to any given date. For example, today is Friday, 1st March 2013. I want my method to return the date of the prior Tuesday: 26th February, 2013. How can I achieve this?
This should do the trick.
var yesterday = DateTime.Now;
while(yesterday.DayOfWeek != DayOfWeek.Tuesday) {
yesterday = yesterday.AddDays(-1);
}
I'd do something like this:
var lastTuesday = DateTime.Today.AddDays(
-1 * (DateTime.Today.DayOfWeek - DayOfWeek.Tuesday));
var lastMonday = DateTime.Today.AddDays(
-1 * (DateTime.Today.DayOfWeek - DayOfWeek.Monday));
This was essentially answered here: Get date of first Monday of the week?
DateTime input = DateTime.Now;
int delta = DayOfWeek.Tuesday - input.DayOfWeek;
DateTime tuesday = input.AddDays(delta);
Related
This question already has answers here:
Rounding up a time to the nearest hour
(6 answers)
Closed 1 year ago.
I need to get the round of current hour, for an example date 28/05/2021 2:16 PM , but i need 28/05/2021 2:00 PM just ignore the minute.
This sould work
DateTime date = DateTime.Now;
DateTime formattedDate = date.AddMinutes(-date.Minute);
Or
DateTime date = DateTime.Now;
DateTime formattedDate = date.Date.AddHours(date.Hour);
to also exclude the seconds
This question already has answers here:
Date Difference in ASP.Net
(6 answers)
Closed 3 years ago.
I organized a program which started on 31st December 2018 at 10:00pm hence its been four months ago, i want a way to find this duration by code.
for example , how youtube is able to tell when a comment was written(eg,4years ago,5 months ago).
You can simply substract a DateTime object from another, which results in a TimeSpan representing the difference:
DateTime x = DateTime.Now;
DateTime y = DateTime.Today;
TimeSpan difference = x - y;
var programStartDateTime = new DateTime(2018, 12, 31);
var timeSpan = DateTime.Now - programStartDateTime;
Console.WriteLine($"The difference is: {timeSpan.ToString()}");
I think below sample code may help you
DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.Now.AddDays(-1);
TimeSpan time = date1 - date2;
WriteLine($"TimeSpan : {time}" );
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I get the DateTime for the start of the week?
I need to get start date of a week based on a input date and period start DayOfWeek. The week may start either from sunday or monday or etc.
//Ger start date based on a date and startdayofweek
GetStartDate(DateTime date, DayOfWeek periodStartDayOfWeek)
{
// return startdateof week
}
What about this?
while (date.DayOfWeek != periodStartDayOfWeek)
{
date = date.AddDays(-1);
}
return date;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calculate previous week's start and end date
I need to get the previous week start date in c#
for eg. today is Jan 9th.
previous week start date is Jan 1th.
I am using the following code
DayOfWeek weekStart = DayOfWeek.Sunday;
DateTime startingDate = DateTime.Today;
while (startingDate.DayOfWeek != weekStart)
startingDate = startingDate.AddDays(-1);
DateTime previousWeekStart = startingDate.AddDays(-7);
Is this the best way in c#
Thanks
As previously stated the duplicate's response will probably do for you
mondayOfLastWeek = DateTime.Now.AddDays( -(int)DateTime.Now.DayOfWeek - 6 );
sundayOfLastWeek = DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek)
Try:
DateTime.Today.AddDays(-7);
This question already has answers here:
How do I get the last day of a month?
(16 answers)
Closed 7 years ago.
How can I find the last day of the month in C#?
Another way of doing it:
DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year,
today.Month,
DateTime.DaysInMonth(today.Year,
today.Month));
Something like:
DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year, today.Month, 1).AddMonths(1).AddDays(-1);
Which is to say that you get the first day of next month, then subtract a day. The framework code will handle month length, leap years and such things.
public static class DateTimeExtensions
{
public static DateTime LastDayOfMonth(this DateTime date)
{
return date.AddDays(1-(date.Day)).AddMonths(1).AddDays(-1);
}
}
DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)
try this. It will solve your problem.
var lastDayOfMonth = DateTime.DaysInMonth(int.Parse(ddlyear.SelectedValue), int.Parse(ddlmonth.SelectedValue));
DateTime tLastDayMonth = Convert.ToDateTime(lastDayOfMonth.ToString() + "/" + ddlmonth.SelectedValue + "/" + ddlyear.SelectedValue);