© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
32 replies
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
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`
        }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How does string.Split() work?
C#CC# / help
3y ago
❔ compare two dates
C#CC# / help
4y ago
❔ Regex split
C#CC# / help
3y ago