C
C#2y ago
SWEETPONY

❔ How to split dates?

I have following:
var dateTime = DateTime.Parse("01.01.2023 0:00:00");
var dArr = new DateTime[47];

dArr = dateTime[0];
for (var i = 1; i < 47; i++)
{
dateTime = dateTime.AddDays(1);
dArr[i] = dateTime;
}

var operatorName = "administrator";
var dataSet = new
{
Department = new
{
OperatorName = operatorName,
Period = $"{dArr[0]} - {dArr[^1]}"
}
}
var dateTime = DateTime.Parse("01.01.2023 0:00:00");
var dArr = new DateTime[47];

dArr = dateTime[0];
for (var i = 1; i < 47; i++)
{
dateTime = dateTime.AddDays(1);
dArr[i] = dateTime;
}

var operatorName = "administrator";
var dataSet = new
{
Department = new
{
OperatorName = operatorName,
Period = $"{dArr[0]} - {dArr[^1]}"
}
}
I added example dataSet to show my issue ok, period in that dataSet will be this: 01.01.2023 0:00:00 - 16.02.2023 0:00:00 but I want another result what I want:
var dataSet = new
{
Department = new
{
OperatorName = operatorName,
Period = $"{dArr[0]} - {dArr[^1]}" // `01.01.2023 0:00:00 - 31.01.2023 0:00:00`
},
Department = new
{
OperatorName = operatorName,
Period = $"{dArr[0]} - {dArr[^1]}" // `31.01.2023 0:00:00 - 16.02.2023 0:00:00`
}
}
var dataSet = new
{
Department = new
{
OperatorName = operatorName,
Period = $"{dArr[0]} - {dArr[^1]}" // `01.01.2023 0:00:00 - 31.01.2023 0:00:00`
},
Department = new
{
OperatorName = operatorName,
Period = $"{dArr[0]} - {dArr[^1]}" // `31.01.2023 0:00:00 - 16.02.2023 0:00:00`
}
}
18 Replies
SWEETPONY
SWEETPONYOP2y ago
how can I split array of datetime in that way?
Pobiega
Pobiega2y ago
Its a bit unsure what you actually want, the sample code you posted isn't valid syntax.
var dateTime = DateTime.Parse("01.01.2023 0:00:00");
var dArr = new DateTime[47];

dArr = dateTime[0];
var dateTime = DateTime.Parse("01.01.2023 0:00:00");
var dArr = new DateTime[47];

dArr = dateTime[0];
that last line isn't valid, as you can't index a datetime what exactly do you want to get from dateTime here? looks like you want your array to be a range between 01.01.2023 and 01.01.2023+47 days? is that correct?
Pobiega
Pobiega2y ago
would something like this work?
Pobiega
Pobiega2y ago
public static IEnumerable<DateOnly> GetDateRange(DateOnly startDate, int days)
{
for (var i = 0; i < days; i++)
{
yield return startDate.AddDays(i);
}
}
public static IEnumerable<DateOnly> GetDateRange(DateOnly startDate, int days)
{
for (var i = 0; i < days; i++)
{
yield return startDate.AddDays(i);
}
}
here is the method used
SWEETPONY
SWEETPONYOP2y ago
yes, my mistake dArr[0] = dateTime; but anyway I have another problem I wanna split this range by month
Pobiega
Pobiega2y ago
this range being the given date + 47 days? and you'd like it "split" by month, so divided into 2-3 lists of dates?
var dateTime = DateTime.Parse("01.01.2023 0:00:00");
var dArr = GetDateRange(DateOnly.FromDateTime(dateTime), 47)
.ToArray();

var byMonth = dArr
.GroupBy(x => x.Month)
.Select(x => x.ToList());
var dateTime = DateTime.Parse("01.01.2023 0:00:00");
var dArr = GetDateRange(DateOnly.FromDateTime(dateTime), 47)
.ToArray();

var byMonth = dArr
.GroupBy(x => x.Month)
.Select(x => x.ToList());
SWEETPONY
SWEETPONYOP2y ago
ah.. it was simple
Pobiega
Pobiega2y ago
there you go, byMonth is now a IEnumerable<List<DateOnly>>
SWEETPONY
SWEETPONYOP2y ago
okey thanks for solution! I appreciate you
Pobiega
Pobiega2y ago
if you need to know the month at a group level, you can just skip the select you'll get an IEnumerable<IGrouping<int, DateOnly>> instead
SWEETPONY
SWEETPONYOP2y ago
thanks I have another maybe stupid question I added model and try to use it
public class FakeBusinessModel
{
public List<Department> Departments { get; set; }
}

public class Department
{
public Operator Operator { get; set; }
}

public class Operator
{
public string? Name { get; set; }
public DateOnly[] Date { get; set; }
}

var obj = new FakeBusinessModel
{
Departments = new List<Department>()
{
new ()
{
Operator = new Operator()
{
Name = "administrator",
Date = GetDateRange(DateOnly.FromDateTime(dateTime), 47)
.ToArray()
},
}
}
};
public class FakeBusinessModel
{
public List<Department> Departments { get; set; }
}

public class Department
{
public Operator Operator { get; set; }
}

public class Operator
{
public string? Name { get; set; }
public DateOnly[] Date { get; set; }
}

var obj = new FakeBusinessModel
{
Departments = new List<Department>()
{
new ()
{
Operator = new Operator()
{
Name = "administrator",
Date = GetDateRange(DateOnly.FromDateTime(dateTime), 47)
.ToArray()
},
}
}
};
I'd like to split operators if date was splitted..
var obj = new FakeBusinessModel
{
Departments = new List<Department>()
{
new ()
{
Operator = new List<Operator>()
{
new()
{
Name = "administrator",
Date = // first month
},
new()
{
Name = "administrator",
Date = // another
}
},

}
}
};
var obj = new FakeBusinessModel
{
Departments = new List<Department>()
{
new ()
{
Operator = new List<Operator>()
{
new()
{
Name = "administrator",
Date = // first month
},
new()
{
Name = "administrator",
Date = // another
}
},

}
}
};
how can I do it?
Pobiega
Pobiega2y ago
var dateTime = DateTime.Parse("01.01.2023 0:00:00");
var dArr = GetDateRange(DateOnly.FromDateTime(dateTime), 47)
.ToArray();

var byMonth = dArr
.GroupBy(x => x.Month)
.Select(x => x.ToList());

var obj = new FakeBusinessModel()
{
Departments = new List<Department>
{
new()
{
Operators = byMonth.Select(x => new Operator()
{
Name = "Admin",
Date = x.ToArray(),
}).ToList()
}
}
};
var dateTime = DateTime.Parse("01.01.2023 0:00:00");
var dArr = GetDateRange(DateOnly.FromDateTime(dateTime), 47)
.ToArray();

var byMonth = dArr
.GroupBy(x => x.Month)
.Select(x => x.ToList());

var obj = new FakeBusinessModel()
{
Departments = new List<Department>
{
new()
{
Operators = byMonth.Select(x => new Operator()
{
Name = "Admin",
Date = x.ToArray(),
}).ToList()
}
}
};
Florian Voß
Florian Voß2y ago
just for my understanding, is it really a good idea to have Operator be a class here? Wouldn't record be the best here?
Pobiega
Pobiega2y ago
I personally greatly prefer records for things like this.
SWEETPONY
SWEETPONYOP2y ago
I've never used record. Why do I need it?
Florian Voß
Florian Voß2y ago
awesome, glad I noticed it. Pretty new to the whole class vs struct vs record class vs record struct deciding 😄 @Pobiega actually we could have Operator and Department be structs and have FakeBusinessModel be a record class? Makes most sense to me but as I've said I'm new to this you don't "need" records, you can create every type as a class wihout issues. But it may have benefits to use record instead in certain circumstances just like you may benefit from choosing a struct over a class in certain circumstances.
Pobiega
Pobiega2y ago
I rarely use structs, unless I'm dealing with things that actually need to be structs, like for networking etc tends to fall into the category of overoptimization
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server