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);
Related
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
This question already has answers here:
How to check if a date has passed in C#?
(6 answers)
Closed 3 years ago.
How would I go about checking if a certain date has passed?
The way this would work is; the program has 3 integers stored
a day, for example: '28' a month: '3' and a year: '2019'.
How would I check if '28-3-2019' has already passed?
The program would return false if the current date is 27-3-2019
and should return true if the current date is 29-3-2019.
The code would be something like:
var date = 28-3-2019
if (date.Passed == true)
{
//do something
}
The date could also be split into 3 ints ofcourse.
DateTime date = new DateTime(year, month, day);
if(date < DateTime.Now)
{
// date has passed.
}
else
{
// the date is in the future
}
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
This question already has answers here:
strtotime equivalent in .NET
(6 answers)
Closed 9 years ago.
Have big problem I don´t know how to compare date in C# like PHP.
I want to convert this code in to C#:
$now = strtotime ("now");
$then = strtotime ("$date");
$difference = $now - $then ;
$num = ($difference/86400)/7;
$weeks = intval($num);
Which function should I use?
This link can´t find a matching function like strtotime.
Please help me to convert the code to C#.
Thanks in advance.
var now = DateTime.Now.TimeOfDay;
DateTime dt = DateTime.Parse(yourDateTime);
var then = dt.ToString("HH:mm");
// here you can do rest of the calculations
basically
dt.ToString("HH:mm"); // 24 hour clock
dt.ToString("hh:mm tt"); // 12 hour clock
Hope it will give you better understanding
I think you might be looking for something like below:
DateTime sDateTimeNow = DateTime.Now;//Gets the date time from the server now
DateTime sIn30Seconds = DateTime.Now.AddSeconds(30);//Gets date time and adds 30 Seconds
DateTime sIn30Hours = DateTime.Now.AddHours(30);//Gets date time and adds 30 hours
DateTime sIn30Days = DateTime.Now.AddDays(30);//Gets date time and adds 30 days
double dTotalDays = (sIn30Days - sDateTimeNow).TotalDays;
double dTotalHours = (sIn30Hours - sDateTimeNow).TotalHours;
double dTotalSeconds = (sIn30Seconds - sDateTimeNow).TotalSeconds;
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