how can i calculate age by datetimepicker [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I calculate someone’s age
How can i calculate age using datetimepicker in c#?

Strictly speaking,
TimeSpan age = DateTime.Now - dateTimePicker.Value;
However, figuring out someone's "age" is only slightly more complicated.
int years = DateTime.Now.Year - dateTimePicker.Value.Year;
if(dateTimePicker.Value.AddYears(years) > DateTime.Now) years--;
Because years vary in length you'll have to do this rather than relying on a structure like the TimeSpan that represents a specific amount of time (the same is true for figuring out how many "months" are between two dates, since months vary in length from 28-31 days).
The last line of code is there to account for the person's birthday not yet taking place this year.

Assuming that the DateTimePicker is called dtpBirthday:
int age = DateTime.Now.Year - dtpBirthday.Value.Year - (DateTime.Now.DayOfYear < dtpBirthday.Value.DayOfYear ? 1 : 0);

Related

Subtracting DateTime Months using .AddMonths(-1) does nothing? [duplicate]

This question already has answers here:
Why DateTime.AddHours doesn't seem to work?
(8 answers)
Closed 2 years ago.
This is my code, and I don't understand why it doesn't work. This is literal copy pasta [edit] from my own code base; there should be no hidden complexities [edit] from elsewhere in the code. This question/answer implies that this should work. What am I missing?
DateTime billDate = new DateTime(2020, 3, 1); // March 1st, 2020
MessageBox.Show($"billdate month {billDate.Month}"); // billdate month 3
billDate.AddMonths(-1); // subtract a month
MessageBox.Show($"month after change {billDate.Month}"); // month after change 3
I expect the month to decrease to 2. Why doesn't it?
AddMonths returns a new DateTime object; it doesn't mutate the existing one. Try:
billDate = billDate.AddMonths(-1);

How to save leap date '02/29/2006' to sql server database? [duplicate]

This question already has answers here:
Leap year calculation
(27 answers)
Closed 4 years ago.
I can' t save this leap date '02/29/2006' to sql server database. I'm using C#. Is there any particular code for this? Your help will be very much appreciated. Thanks
Maybe it's because it's not a leap year? :)
2006 is not a leap year. Leap years occur usually every four years (those years whose number is divisible by four) except for century years (divisible by 100) whose number is not divisible by 400. 2006 is not divisible by 4, so it fails the first condition.
You Cant save it - Because it wasnt a leap year:

Divide Time by two [duplicate]

This question already has an answer here:
Dividing HH:MM:SS by an integer
(1 answer)
Closed 5 years ago.
Is there a easy solution for dividing a time? For example divide 04:00:30 by two and get 2:00:15?
select FIRMA, NR, CAST(Zeit AS TIME(0)) as Zeit
from [2_2017]
where Art='D'
and Zeit is not Null
and Zeit !='00:00:00'and Tour_1 ='0'
order by Firma,NR
Perhaps you first need to convert time to some integer as sec, min , hours from midnight and then devide it and add back to midnight
DECLARE #zeint time = '23:00:15'
declare #midnight time ='00:00:00'
select
dateadd(SECOND,
(DATEDIFF(SECOND,#midnight, #zeint)/2)
,#midnight) as half

How can I add 1 minute to current time that I get? webdriver c# [duplicate]

This question already has answers here:
Add hours or minutes to the current time
(4 answers)
Closed 7 years ago.
I get the current time and need to add 1 minute to it so It would be 1 minute ahead, how do I do that?
string date1 = System.DateTime.Now.ToString("HH:mm");
All you have to use is DateTime.AddMinutes
string date1 = System.DateTime.Now.AddMinutes(1).ToString("HH:mm");
Conversely, if you want to subtract a minute just use a negative parameter.
DateTime.AddMinutes method can do that.
If you want a string representation then do:
string date1 = System.DateTime.Now.AddMinutes(1).ToString("HH:mm");
As a side note, you can also supply a negative number to subtract minutes from current time like:
DateTime.Now.AddMinutes(-1)

Calculate no of days remaining [duplicate]

This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Closed 9 years ago.
I want to calculate no of days remaining for particular day from today onwards. How to calculate difference between 2 dates in C#??
(Note: This answer was written when the question was worded very differently)
Assuming your dates are DateTime or DateTimeOffset:
TimeSpan delta = date1 - date2
TimeSpan GetDelta(DateTime d1, DateTime d2)
{
return (d1 - d2).TotalDays;
}

Categories