Finding the date of monday in a week with asp.net [duplicate] - c#

This question already has answers here:
DateTime question in VB.NET
(3 answers)
Closed 9 years ago.
I need to find the date of Monday this week.
For example, for this week Monday was on the 25th, so the date I need is: 25/02/2013
And when we roll over to next week it needs to calculate: 03/03/2013.
I tried to search but i cant find it for asp.net.

DateTime mondayDate = DateTime
.Today
.AddDays(((int)(DateTime.Today.DayOfWeek) * -1) + 1);
So for Today {27/02/2013 12:00:00 AM} it will give {25/02/2013 12:00:00 AM}

Usage in C#:
dt.AddDays(1 - (dt.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)dt.DayOfWeek));
Usage in VB.NET:
dt.AddDays(1 - If(dt.DayOfWeek = DayOfWeek.Sunday, 7, dt.DayOfWeek))
Reference: DateTime question in VB.NET

Related

How can we get Monthly(Day-Of-Week) for backup purpose [duplicate]

This question already has answers here:
Get DateTime of the next nth day of the month
(5 answers)
How to calculate 2nd Friday of Month in C# [duplicate]
(4 answers)
Closed 3 years ago.
I want to get a next backup date using (day-of-week) monthly for a scheduled backup.
If I took a backup on Monday, second week, Jan-2019, then Next backup date should be Monday in the second week of February-2019.
So, How can we get the specific day as per the given week for every coming month?
Now I'm getting the next backup month but I'm not sure how we get the same Day in a specfic week.
You should create an extension method for DateTime class.
public static DateTime NthOf(this DateTime CurDate, int Occurrence , DayOfWeek Day)
{
var fday = new DateTime(CurDate.Year, CurDate.Month, 1);
var fOc = fday.DayOfWeek == Day ? fday : fday.AddDays(Day - fday.DayOfWeek);
if (fOc.Month < CurDate.Month) Occurrence = Occurrence+1;
return fOc.AddDays(7 * (Occurrence - 1));
}
And use
new DateTime().NthOf(2, DayOfWeek.Monday))
Refer: How to calculate 2nd Friday of Month in C#
Why dont you get the DayOfTheYear and just add 7 to it. Something like this:
var dateTime = DateTime.Now;
var dayOfYear = dateTime.DayOfYear;
var nextDate = dateTime.AddDays(dayOfYear + 7);
Console.WriteLine(nextDate);
Since the next backup day is the same day, which means just add 7 to it. You will have to manage the last week of year though in this case

Get first three characters from DayOfWeek , Month [duplicate]

This question already has answers here:
How to get the first five character of a String
(22 answers)
Closed 5 years ago.
I've a datetime:
DateTime dt = new DateTime(2003, 5, 1);
dt.DayOfWeek // returns Thursday
How I can split only first three characters from DayOfWeek e.g. Thu?
If you mean for it to work in different cultures, then your best bet is probably:
var abbr = culture.DateTimeFormat.GetAbbreviatedDayName(dayOfWeek);
where culture is a CultureInfo, for example:
var culture = CultureInfo.CurrentCulture;
In English cultures, this is the 3-letter version:
Sun
Mon
Tue
Wed
Thu
Fri
Sat
Marcs approach is the best here. But if i understand your question in a more general way, how to get the first three letters of an enum-value, you can use string methods if you use enum.ToString:
DateTime dt = new DateTime(2003, 5, 1);
string dow = dt.DayOfWeek.ToString();
dow = dow.Length > 3 ? dow.Remove(3) : dow;

Calculating for remaining days in current month [duplicate]

This question already has answers here:
How to find the first day of next month,if the current month is december
(12 answers)
Calculate difference between two dates (number of days)?
(17 answers)
Closed 5 years ago.
I would like to know how to calculate the remaining days of the month e.g (15 Feb 2017 to 28 Feb 2017) without the use of a datetimepicker set at 28 Feb 2017.
Here are my codes for subtraction of 2 datetimepicker:
DateTime startDate =
(DateTime)dateTimePicker2.Value;
DateTime endDate =
(DateTime)dateTimePicker1.Value;
TimeSpan ts = endDate.Subtract(startDate);
textBox10.Text = ts.Days.ToString();`
Here are the steps you need to go through:
take your current date
use DateTime.AddMonths() to generate a new date one month from your current date
create a new date that uses 1 for the day, and the month and year from the future date you just worked out
subtract your current date from the future date, this will give you a Timespan which contains the number of days difference
You can use the closely related question Calculate difference between two dates (number of days)? as a guide.
Here is an example, but you can use the Value property from your DateTimePicker instead. DateTime.DaysInMonth(int year, int month) is a helpful method.
DateTime beginDate = new DateTime(2017, 2, 15);
var daysLeft = DateTime.DaysInMonth(beginDate.Year, beginDate.Month) - beginDate.Day;
Console.WriteLine("days from Feb 15 to Feb 28: {0}", daysLeft);
Output:
days from Feb 15 to Feb 28: 13
You can try:
DateTime dt = DateTime.Now;
int days = dt.AddDays(1 - dt.Day).AddMonths(1).AddDays(-1).Day - dt.Day;

check if a date falls in the next 24 hours with c # [duplicate]

This question already has answers here:
How to know if a DateTime is between a DateRange in C#
(7 answers)
Closed 7 years ago.
it is possible with C # test whether the DateTime " 01/29/2016 16:22:00 " is included in the next 24 hours? I have a date that is " 01/28/2016 15:30 " ( DateTime.Now ) and I 'd like to know if it is within the range going forward 24 hours. I hope I explained .
(yourDatetime - DateTime.Now) <= TimeSpan.FromHours(24)
You can use AddHours:
var myDateTime = ...
var now = DateTime.Now;
if (myDateTime <= now.AddHours(24)
&& myDateTime >= now.AddHours(-24))
{
}
Also keep in mind that DateTime is immutable and they cannot be modified after creation. That is why AddHours returns a new instance.
Update:
After seeing M.kazem's answer I have though that you can also use that:
Math.Abs(myDateTime.Subtract(DateTime.Now).TotalHours) <= 24

Alternate AddMonth functionality [duplicate]

This question already has answers here:
How can I get the last day of the month in C#? [duplicate]
(5 answers)
Closed 8 years ago.
A person enters a date into the system, is there he selects February 28th, it reproduces march 28th. But my goal is to grab end of the month, March 31st. Is there a way to move to end of month for next month without incorporating the snippet below, in other words a cleaner method?
if (Date.Month == 2)
Date.AddDays(31)
else if (Date.Month == 3)
Date.AddDays(30)
etc...
any tips or suggestions would be great, clean code is always the best code.
var d = new DateTime(...);
var newDate = new DateTime(d.Year, d.Month, 1).AddMonths(2).AddDays(-1);
this should also work:
var newDate = new DateTime(d.Year,1,31).AddMonths(d.Month-1);

Categories