date not incrementing with AddDays(1) [duplicate] - c#

This question already has answers here:
AddDays() not working within a while loop
(3 answers)
Closed 8 years ago.
I'm writing a loop that go through all the week. It looks like this:
DateTime TheDate = DateTime.UtcNow.AddDays(-365);
while(TheDate.Date <= DateTime.UtcNow.Date)
{
TheDate.AddDays(1);
if (TheDate.DayOfWeek == 5 || TheDate.DayOfWeek == 6)
{
continue;
}
SomeMethod(TheDate);
}
The problem is that TheDate never gets incremented. Why is that and how do I fix it?

AddDays returns new instance of DateTime, so:
TheDate = TheDate.AddDays(1);

Related

How do I check if a certain date has passed in C#? [duplicate]

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
}

Datetime comparison [duplicate]

This question already has answers here:
How to compare DateTime in C#?
(9 answers)
Closed 7 years ago.
My program is reading data from SQL Server like this:
if (!reader.IsDBNull(4))
{
message.WazneDo = reader.GetDateTime(4);
}
This code read datetime from SQL Server. Then I will load it to datagridview:
WypozyczZwrocDb _dost1 = new WypozyczZwrocDb();
Global.listWypozyczZwroc = _dost1.PokazZar();
Global.fMain.Tabela.DataSource = Global.listWypozyczZwroc;
How to compare with today's date? I want change the row color.
I tried:
DateTime dzis = DateTime.Now;
if(Tabela.Rows[Tabela.CurrentCell.RowIndex].Cells[4].Value > dzis)
if(Tabela.Rows[Tabela.CurrentCell.RowIndex].Cells[4].Value > dzis.ToString())
DateTime.Compare() will allow you to use comparative operators in a conditional statement based on the int returned.
DateTime.Compare() msdn

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

Making a DateTime? from two strings for date and time [duplicate]

This question already has answers here:
How to combine two strings (date and time) to a single DateTime
(9 answers)
Closed 7 years ago.
My input I recieve two strings:
string1 = "12/15/2015"
string2 = "22:45"
How can I combine them to have a DateTime? value.
Use this.
DateTime dt = Convert.ToDateTime(string.Format("{0} {1}",string1,string2));

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