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);
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:
how get yesterday and tomorrow datetime in c#
(9 answers)
Closed 5 years ago.
I have two variables
startdate
enddate
In C#, I want to specify in variables startdate and enddate where the date is equal to yesterday.
For example result I want to achieve:
currentday = '14.09.2017'
startdate = '13.09.2017'
enddate = '13.09.2017'
Try this:
System.DateTime today = System.DateTime.Now; System.DateTime yesterday = today.AddDays(-1);
How about
startdate = DateTime.Now.AddDays(-1)
Doesn't that give you exactly what you want?
You have to use DateTime.Now.Date.AddDays(-1) for yesterday and DateTime.Now.Date.AddDays(1) for tomorrow.
This question already has answers here:
How to subtract one year from DateTime [closed]
(4 answers)
Closed 8 years ago.
I am trying to find a date (string) that is 5 years from current date in the past:-
DateTime.UtcNow.Date - DateString = 5 years
Can anyone help me formulate this into c# syntax?
var myDate = DateTime.UtcNow;
var newDate = myDate.AddYears(-5);
You can add or subtract a TimeSpan from a DateTime object to get another DateTime object.
var fiveYearsAgo = DateTime.Now - TimeSpan.FromYears(5);
var fiveYearsFromNow = DateTime.Now + TimeSpan.FromYears(5);
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);
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;