Home > Net >  Random date and time C# from last week
Random date and time C# from last week

Time:02-05

I am trying to generate a start and end date time from last week.

so if today is 04/02/2022 then I need a random date and time from last week so it could be

Start 02/02/2022 10:00

End 02/02/2022 10:30

notice that the end time is 30 mins always.

I can get the date part working by using this

Random gen = new Random();
var start = DateTime.Now.AddDays(-7);
int range = (DateTime.Today - DateTime.Today.AddDays(-7)).Days;

var result = start.AddDays(gen.Next(range));

CodePudding user response:

This should work:

Random gen = new Random();
var start = DateTime.Now.AddDays(-7);
start = start.AddDays(gen.Next(6)); // Add 0-6 days
start = start.AddHours(gen.Next(23)); // Add 0-23 hours
start = start.AddMinutes(gen.Next(1)); //Add 0-1 minutes.

var result = new DateTime(
    start.Year,
    start.Month,
    start.Day,
    start.Hour,
    start.Minute % 2 == 0 ? 0 : 30, // If even then zero, if odd then 30.
    0); // 0 seconds

CodePudding user response:

That's kind of a fun challenge. Personally I like simple solutions:

var halfHoursPrWeek = 7 * 24 * 2;

// Get last full hour; ex: 04.02.2022 15:00:00
var previousHour = DateTime.Today.AddHours(DateTime.Now.Hour);
var randomNrOfHalfHours = (new Random()).Next(halfHoursPrWeek);

// ex: 31.01.2022 20:30:00
var sometimeLastWeek = previousHour.AddMinutes(-randomNrOfHalfHours * 30);

CodePudding user response:

As I said in the comment already, generate a DateTime object based on the ticks.

//startDate 7 days ago, without time so 00:00:00
var startDate = DateTime.Now.Date.AddDays(-7);

//endDate with the time of 23:59:59
//notice manipulating the time this way could lead to a DateTime in the future, if you don't want that only use `DateTime.Now`
var endDate = DateTime.Now.Date.AddDays(1).AddMilliseconds(-1);

//ticks in range of the dateTimes
var ticks = new Random().NextInt64(startDate.Ticks, endDate.Ticks);

//new DateTime
var newDate = new DateTime(ticks);

Console.WriteLine($"NewDate in the range of {startDate} and {endDate}:");
Console.WriteLine(newDate);

https://dotnetfiddle.net/HcyAUD

CodePudding user response:

See following :

            const int MINUTES_PER_WEEK = 7 * 24 * 60;

            DateTime date = DateTime.Now;

            DateTime SundayMidnight = date.AddDays(-(int)date.DayOfWeek).Date;
            DateTime startDate = SundayMidnight.AddDays(-7);

            Random rand = new Random();

            int randomMinutes = rand.Next(MINUTES_PER_WEEK - 30);
            DateTime randomStartDate = startDate.AddMinutes(randomMinutes);
            DateTime randomEndDate = startDate.AddMinutes(randomMinutes   30); 

CodePudding user response:

Calculation through ticks.

// One week interval in ticks.
const long WEEK = 6048000000000; 

// Initial date & time.
DateTime now = DateTime.Now;

// Random decrease interval of 1 week.
Random gen = new Random();
long reduce = Convert.ToInt64(gen.NextDouble() * WEEK); 
TimeSpan span = TimeSpan.FromTicks(reduce);

// Determine a random date at last week.
DateTime lastWeek = now - span;
  •  Tags:  
  • Related