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.
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 8 months ago.
Improve this question
Let's say I have two values (lower boundary = 0.5, upper boundary = 100.0). Then I want to split this range 40 times so that the first is 0.5 and the 40th is 100.0. How can I find the 38 values between 0.5 and 100.0 so that the distance between the values is always the same?
This sounds like a homework? You should be really able to solve this simple problem.
Always split your task into steps. Before you think of any code, you must understand the problem. Then find a solution. You can phrase the solution in words and pseudo code. Finally, you can try to implement this solution:
First you must find the appropriate algorithm:
Identify the input and the output (result):
a) calculate the range of your lower_bound=0.5 (input) and upper_bound=100 (input): range=upper_bound-lower_bound
b) Then divide range by steps=40 (input) to get the interval.
c) Finally, walk from lower_bound to upper_bound by incrementing each step by interval to get each value that belongs to the range.
Implement the algorithm
void Main()
{
double steps = 40;
double lowerBound = 0.5;
double upperBound = 100;
double range = Math.Abs(upperBound - lowerBound);
double interval = range / steps;
var rangeValues = new List<double>();
for (double step = lowerBound; step <= upperBound; step += interval)
{
rangeValues.Add(step);
}
}
Test and improve the algorithm to improve performance and readability. For example rename variables and move code into methods. by moving code into methods that accept the input as parameters, you can convert the hard-coded algorithm into a dynamic one.
Make the algorithm robust (important).
For example, you must ensure that the variable steps is never zero to avoid an DivideByZeroException exception. For this reason you must validate the input before you can run the algorithm.
This question already has answers here:
c# get the min value from array
(3 answers)
Closed 2 years ago.
I'm getting from the user 20 Numbers and I need to print back the array AND ALSO lowest, minimal number and its position in the array. I am wondering if my code is correct. Can you tell any typos?
P.S., it's my first week of coding, so I am kind of a "Noob", which means that my code will be pretty bad.
Links to similar problems will be appreciated as well! :)
Console.WriteLine("Hello! Enter your very important Numbers!");
int[] Numb = new int[20];
for (int i = 0; i < 20; i++)
{
Console.WriteLine("Enter Numbers here!");
Numb[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine(Numb);
Console.WriteLine(Math.Min(Numb));
Printscreen with my error(s):
To find out Minimum number from an array you need to use .Min() function from System.Linq
Returns the minimum value in a sequence of values.
using System.Linq;
...
Console.WriteLine(Numb.Min()); //Instead of Math.Min(Numb);
.NET Fiddle
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 3 years ago.
Improve this question
I want to find difference between two DateTime objects. I know I can use DateTime.Subtract() and overloaded operator '-' (e.g DateTime - DateTime). But someone told me that these two techniques are not good enough, and there is a method DateDiff or something, which I should use and which is best to use. I want to know if it is true? If it is, then how the above two are inferior from any perspective?
DateDiff does not compare dateTime instances.
It compares specific parts of the DateTime instances - for example, the months, or hours.
For instance:
var lasyDayOf2018 = new DateDime(2018, 12, 31);
var firstDayOf2019 = lasyDayOf2018.AddDays(1);
var monthDiff = DateDiff(DateInterval.Month, lasyDayOf2018, firstDayOf2019)
monthDiff will be 1, even though only one day has passed between the two dates.
It's documented in the Remarks section of the DateDiff documentation page:
If Interval is set to DateInterval.Year, the return value is calculated purely from the year parts of Date1 and Date2. Similarly, the return value for DateInterval.Month is calculated purely from the year and month parts of the arguments, and for DateInterval.Quarter from the quarters containing the two dates.
For example, when comparing December 31 to January 1 of the following year, DateDiff returns 1 for DateInterval.Year, DateInterval.Quarter, or DateInterval.Month, even though at most only one day has elapsed.
To get the actual difference between different instances of DateTime, either use Subtract or -, as you wrote in the question.
Whether or not there is a problem, and the solution to that problem really depends on the situation and why you're computing the difference. There are two main problems with DateTime.Subtract() and operator -.
The first is that they both ignore the time zone portion of the data. So, for example, if you have two dates, 2019-07-18 12:00:00 UTC and 2019-07-18 06:00:00 MDT (local time), even though they represent the same moment in time (as measured from different time zones), DateTime.Subtract() will tell you that they're 6 hours apart.
The second problem is that you often want to know the answer in terms of units other than ticks, sometimes even something that cannot be converted to a fixed number of ticks. For example, if you want the difference in the months represented by two DateTimes, you cannot compute that from the difference in ticks (which is what DateTime.Subtract() is based on), because different months have different lengths. For example, for the dates '2019-06-30 23:59:59' and '2019-07-01 00:00:00' (in the same time zone) there is only a one second difference, but the months are different, so is the difference in months one or zero? It depends on what you want to use the answer for.
This question already has answers here:
Add 1 week to current date
(3 answers)
Closed 8 years ago.
I want to add number of days with the current date. Help me to find a proper solution. Thank you.
Code:
string s = DateTime.Now.ToString();
I want to add 2 days with the current date.
string s = DateTime.Now.AddDays(2).ToString();
UPDATE
In answer to your comment
string s = DateTime.Now.AddDays(2).ToShortDateString();
DateTime.Now.AddDays(2).ToString();
You can use the AddDays method on the DateTime struct. You can use that method to add or substract any amount of days from the current date, like in this sample.
DateTime added = DateTime.Now.AddDays(2);
string s = added.ToString();
Or even:
DateTime substracted = DateTime.Now.AddDays(-2);
Note that if you are only interested in the date, you could use DateTime.Today rather than DateTime.Now since that will be a little more performant.
A final note on the use of ToString: the output of ToString may differ when the OS running on uses different cultures. If you intend to process this outputted string later on, I suggest to pass in the desired culture, using this overload of ToString.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.NET String.Format() to add commas in thousands place for a number
I have an int that is passed through to the a view and it need to formatting so it is not one big block of number. e.g
0
00
000
000,0
000,000
000,000,000
How can i do this using a for loop?
thanks
You may want to output the integer as i.ToString("###,###,###");
However, this would not display a value if the int is 0.
You could try using System.Drawing.Color to represent the colour value rather than an int. This has a method ToArgb()