Hi guys! Is it posible to get the date (yyy-MM-dd) of a particular day, say today is friday and i want to get the date last monday. Is it posible? how? Thanks!
Printable View
Hi guys! Is it posible to get the date (yyy-MM-dd) of a particular day, say today is friday and i want to get the date last monday. Is it posible? how? Thanks!
First of all forget any talk of format. DateTime objects have no format. They are just a date and time value. No date or time calculation needs to, or should, consider format. Format is only a consideration when displaying a DateTime, which means when converting it to a string. You can convert any DateTime value to a stri8ng in any format you want. That's a given, so don't even consider it when talking about date/time calculations.
You can use the DayOfWeek enumeration. Monday has a numerical value of 1, so you can get the numerical value of the current day and and then subtract the difference:Just note that if today is Sunday then that calculation will give you tomorrow's date, because Sunday is considered the first day of the week. If you want to use some other rule then you can perform the appropriate translation.C# Code:
DateTime mondayThisWeek = DateTime.Today.AddDays(-DateTime.Today.DayOfWeek + 1)
Sorry for that, thewas misinterpreted or I just used it in a wrong way, the reason why I put theQuote:
(yyy-MM-dd)
is to emphasize the date because may be some would think that it is a day e.g Monday, tuesday, etc...My mistake...Anyway, thanks a lot JM!Quote:
(yyy-MM-dd)