Datetime comparison [duplicate] - c#

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

Related

C#: DateTime form string [duplicate]

This question already has answers here:
String was not recognized as a valid DateTime " format dd/MM/yyyy"
(13 answers)
datetime.parse and making it work with a specific format
(2 answers)
Closed 2 years ago.
So I have this `string:
string time = "20201006 10:42:42.925"
Which look like valid time format.
And this is what I have try:
DateTime dt1 = DateTime.Parse(time);
And got this error: String was not recognized as a valid DateTime.
Use TryParse Method, eg
dateTime dt1;
if DateTime.TryParse(time, out dt1)
{
// all good
} else{
// no good
}

converting datetime to string with T07:00:00.0000000Z [duplicate]

This question already has answers here:
Given a DateTime object, how do I get an ISO 8601 date in string format?
(18 answers)
Closed 4 years ago.
I am attempting to query table storage by generating the following query:
var date = new DateTime(1954, 9, 7);
var timequery = TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.LessThanOrEqual, date.ToString());
I am getting a bad request when simply doing date.ToString()
The string that I need would be in the following format: 1954-09-07T07:00:00.0000000Z
How do I convert a regular DateTime to be a string in the specified format?
Var date = new DateTime(1954,7,0,0,0,0,DateTimeKind.Utc);
Var stringDate = date.ToUniversalTime().ToString(“o”);
Gives you the required result as
1954-09-07T00:00:00.0000000Z

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));

How can i subtract a datetimepicker from current date in C# .net? [duplicate]

This question already has answers here:
How to convert date format to DD-MM-YYYY in C#
(16 answers)
Closed 7 years ago.
TimeSpan result = DateTime.Today.Subtract(birthDaydateTimePicker.Value);
resultTextBox.Text = result.ToString();
I want the result to show as dd/mm/yyyy format.
This should work, your subtracting dates looks fine assuming your parameter is a date value
{
TimeSpan result = DateTime.Today.Subtract(birthDaydateTimePicker.Value);
resultTextBox.Text = result.ToString("dd/MM/yyyy");
}
Subtracting dates
Changing the format of a date

date not incrementing with AddDays(1) [duplicate]

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);

Categories