Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can anybody tell me what is the difference between these two functions in C#? TotalDays and Days because I'm not sure which once I should use in my code? Sorry for the low information on this text, but there is not much I can talk about.
Since i haven't found a duplicate i post my comment here:
Always read the documentation first. TotalDays is a double because it represents whole and fractional days whereas Days is an int which represents only whole days.
That is even mentioned explicitly in the remarks sections of TimeSpan.Days/TotalDays:
The Days property represents whole days, whereas the TotalDays
property represents whole and fractional days.
One thing to note, as opposed to the other properties in TimeSpan like Hours/TotalHours there is no limit on Days. So it doesn't end with 30 or 365(like Hour which ranges from -23 through 23) since there is no larger unit than year. So Days will always be the same number as (int) ts.TotalDays.
A TimeSpan doesn't have a sensible concept of "years" because it
depends on the start and end point. (Months is similar - how many
months are there in 29 days? Well, it depends...) [J. Skeet]
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a DateTimePicker and two buttons on a form. The buttons are intended to allow a user to cycle backwards and forwards through the dates displayed in the picker.
The code DateTimePicker.Value.AddDays(1); increments the value displayed and DateTimePicker.Value.AddDays(-1); decrements it. It seems a bit clunky to me but this works as expected, is passing in a value of -1 the correct way to decrement the displayed date?
Why isn't there a SubtractDays() method?
As you've seen, you can use AddDays with a negative amount to subtract days, so there's no need for the extra methods (there would need to be one for each of the Add methods). If it really bothers you, you can write extension Subtract methods for all the Add methods.
For example
public static class DateTimeExtensions
{
public static DateTime SubtractDays(this DateTime start, int days)
{
return start.AddDays(-days);
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
So this worked for the last few years:
int currentYear = new DateTime().Year % 100;
But this year instead of 18 it returns 1.
I've fixed it with a different bit of code but why did it suddenly stop working? What's special about 2018?
Edit: Typo in the code provided to me, should have checked it myself. Lesson learned. Thanks to those who answered and feel free to delete / close the question.
Because
int year = new DateTime().Year;
returns 1. This behaviour is described in the Documentation:
DateTime dat1 = new DateTime();
// The following method call displays 1/1/0001 12:00:00 AM.
Console.WriteLine(dat1.ToString(System.Globalization.CultureInfo.InvariantCulture));
// The following method call displays True.
Console.WriteLine(dat1.Equals(DateTime.MinValue));
To get the year you need to use the current time. Use
int currentYear = DateTime.Now.Year % 100;
to solve your problem.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have made a calendar, now I want user to choose the days. For example there is a calendar of month May. User wants to see all the dates for Monday column (e.g. Mondays of the month : 5,12,19,26) <-- as Monday falls in these days of the month. Users is allowed to chose by using Switch statement and case from 0 to 6.
My question is how can I retrieve the days from a column that user chose and display it like in the example (e.g. Mondays of the month : 5,12,19,26). I don't want an answer in code. I just want somebody to give me an idea on how can I do this or hints in code with explanation, not straight forward answer.
I use 2d array to make calendar table. 2d array contains columns and rows that are required by me. I am also using single array to hold string of days that act as a column for they days that 2d array holds. (E.g May
Mon Tues...
1 2...)
Thanks.
I think the best way is to handle the click of the user. After that you can just add/substract 7 (number of days in a weel) to the day he chooses. Then make sure to not exceed (>31 or <1) the number of days in a month.
private void dateTimeInput1_ValueChanged(object sender, EventArgs e)
{
DateTime selectedDate = dateTimeInput1.Value;
DateTime theNextWeek = selectedDate.AddDays(+7);
DateTime thePreviousWeek = selectedDate.AddDays(-7);
}
All you have to do is select all the weeks.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I was wondering what is exactly the value of gameTime.ElapsedGameTime.TotalSeconds in a second?
gametime.ElapsedGameTime returns the time elapsed since the last update, not the total game time. For this, you need gametime.TotalGameTime.
It is returned as a TimeSpan, so the last part (TotalSeconds) is a property of that struct. Since it's a TimeSpan, you have full access to other properties, like TotalMilliseconds or methods like Compare.
And since TotalSeconds is a double, it will indeed be 0.5 if the elapsed time is 500 milliseconds.
If the game is running at 60 frames per second the value is likely to be 1 / 60 = 0.0166 seconds.
gameTime.ElapsedGameTime.TotalSeconds, as the name suggests, is the total number of seconds your game has been running. So if your game has been running for 1 second then this will be equal to 1. If your game has been running for a minute then this will be equal to 60.
Is there more to your question?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let's suppose I have an interval. Say 15 minutes.
I have a start time of 13:57 and an end time of 15:17.
The time when this process runs happens to be 14:07.
I want the result to be 14:00,14:15,14:30,14:45,15:00,15:15 while retaining the year/month/date, etc.
So far, I have these facts down. The minutes modulo the interval is always zero. I need to count down from the current time until I hit the first mod-zero number which is 14:00.
I then simply increase that number by the interval until I reach my ceiling. My real question is how to come up with an elegant, simple way to find this first floor number.
The interval is a timespan and the other two values are datetimes.
Any ideas?
You can calculate the minute for the first result instance like:
m: current time's minute
new minute part: m - (m % interval)