This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Closed 6 years ago.
My sysadmin has failed to actually expire passwords in AD and now my password expiry warning system is starting to count back up instead of showing negative days. I'm expecting remainingDays to be a negative number for my test account ( 5 days pseudo-expired) from the following code, hoping someone can show me why I'm losing the negative. From what I've read on MSDN, DateTime.Subtract can return negative values.
DateTime today = DateTime.Now;
foreach (User user in users)
{
DateTime expiryDate = user.pwdLastReset.AddDays(180); //pwd expires every 180 days
int remainingDays = Int32.Parse(expiryDate.Subtract(today).ToString("%d"));
//snipped code to send warnings at different days remaining.
}
DateTime.ToString("%d") will actually return the day of the month represented. For example, the following code:
DateTime prev = DateTime.Now.Subtract(TimeSpan.FromDays(10));
Console.WriteLine(prev.ToString("%d"));
prints out "15" (as in, "July 15, 2016"), not 10 or -10.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to compare the current hour to see if it's past 16 h (4 o clock in the afternoon)
int temps = new TimeSpan(16, 0, 0).Hours;
int now = DateTime.Now.TimeOfDay.Hours;
int result=1;
if (now > temps)
result = 2;
but if I want now to be displayed like this 14h or 15h without having to do this
now.ToString("MM/dd/yyyy HH:mm")
because I need to compare it to temps so I need it as an integer
is it possible? All my research led to advising me to use the to string format
I have tried : int now = DateTime.NowUTC.TimeOfDay.Hours; which displayed 17h but the problem is that it's the time in the UK and I live in Canada
A DateTime is always stored the same way internally, no matter how it is displayed. Whenever you see a date, it means that is has been converted to a string and formatted. Even in the debugger! What you see is just a temporary representation of the real DateTime stored as a number.
The documentation of the DateTime.Hour Property says:
The value of the Hour property is always expressed using a 24-hour clock.
This real DateTime is internally stored as the time which elapsed since a reference date. The documentation of the DateTime Structure says:
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar (excluding ticks that would be added by leap seconds). For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight.
You can write
int now = DateTime.Now.Hour; // Always expressed using a 24-hour clock.
int result = 1;
if (now > 16)
result = 2;
Or more concise
int result = DateTime.Now.Hour > 16 ? 2 : 1;
Don't use NowUTC if you want your local time. This yields what was once called Greenwich Mean Time (GMT).
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
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)
This question already has answers here:
Random DateTime between range - not unified output
(5 answers)
Closed 6 years ago.
I am automating an application where records can be opened between specified date ranges. I know that I can create an array that randomly picks a number in my array, such as:
DateTime.Now.AddDays(90).ToString("MM/dd/yyyy")
However, I would like to know if there is a better way to go about this. Please help! Thanks.
void Main()
{
Console.WriteLine(DateTime.UtcNow.AddDays(new Random().Next(90)));
}
This will add a random amount of days to the start date, essentially ending up with a random date.
Assuming that you want the dates to be in the range of 90 days of the date of generation. Then, you can try this:
int seed = (int)DateTime.Now.Ticks;
int days = seed % 90;
DateTime.Now.AddDays(days).ToString("MM/dd/yyyy");
You can, of course, refactor this. I was verbose for the sake of clarity. You can also change the integer value of 90 to any integer you want to be the upper-bound of your range.
Hope this helps.
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);