Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Last active March 16, 2025 19:12
Show Gist options
  • Save karenpayneoregon/c2f212ce25e5178b3554fedba218ea9f to your computer and use it in GitHub Desktop.
Save karenpayneoregon/c2f212ce25e5178b3554fedba218ea9f to your computer and use it in GitHub Desktop.
How AI can assist a developer

An excellent use of AI assistants like Copilot and ChatGPT is when performing conversations.

In the code below, the task was to convert methods returning DateTime to DateOnly.

Using an AI assistant can be considered a timesaver. In this case, I’m sure most readers would have no problem performing the conversion themselves, but why not let an assistant do this in seconds, unlike a developer taking minutes or longer?

Copilot is free as is ChatGPT. Personally, I want both, as each has advantages and disadvantages. I like the ability to create projects and custom GPTs with the Teams version of ChatGPT.

public static class DateTimeHelpers
{
/// <summary>
/// Generates a list of dates representing the next week's dates starting from the upcoming Sunday.
/// </summary>
/// <returns>A list of <see cref="DateOnly"/> objects representing the dates of the next week.</returns>
public static List<DateOnly> NextWeeksDates()
{
var start = DateTime.Now;
var nextSunday = DateOnly.FromDateTime(start).Next(DayOfWeek.Sunday);
return Enumerable.Range(0, 7)
.Select(index => nextSunday.AddDays(index))
.ToList();
}
/// <summary>
/// Generates a list of all days in the specified month of the current year.
/// </summary>
/// <param name="month">The month for which to generate the list of days (1 to 12).</param>
/// <returns>A list of <see cref="DateOnly"/> objects representing each day in the specified month.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when the <paramref name="month"/> is not in the range 1 to 12.
/// </exception>
public static List<DateOnly> GetMonthDays(int month)
{
int year = DateTime.Now.Year;
return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
.Select(day => new DateOnly(year, month, day))
.ToList();
}
/// <summary>
/// Divides the days of a specified month and year into weeks, where each week is represented as a list of days.
/// </summary>
/// <param name="year">The year of the month to process.</param>
/// <param name="month">The month to process (1 to 12).</param>
/// <returns>A list of weeks, where each week is a list of <see cref="DateOnly"/> objects representing the days in that week.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when the <paramref name="month"/> is not in the range 1 to 12.
/// </exception>
public static List<List<DateOnly>> GetWeeksInMonth(int year, int month)
{
List<List<DateOnly>> weeks = [];
DateOnly firstDay = new(year, month, 1);
DateOnly lastDay = new(year, month, DateTime.DaysInMonth(year, month));
List<DateOnly> currentWeek = [];
for (var day = firstDay; day <= lastDay; day = day.AddDays(1))
{
if (day.DayOfWeek == DayOfWeek.Sunday && currentWeek.Count > 0)
{
weeks.Add(currentWeek);
currentWeek = [];
}
currentWeek.Add(day);
if (day == lastDay)
{
weeks.Add(currentWeek);
}
}
return weeks;
}
/// <summary>
/// Calculates the next occurrence of the specified day of the week, starting from the given date.
/// </summary>
/// <param name="from">The starting date to calculate from.</param>
/// <param name="dayOfWeek">The target day of the week to find.</param>
/// <returns>A <see cref="DateOnly"/> object representing the next occurrence of the specified day of the week.</returns>
public static DateOnly Next(this DateOnly from, DayOfWeek dayOfWeek)
{
int start = (int)from.DayOfWeek;
int target = (int)dayOfWeek;
if (target <= start)
{
target += 7;
}
return from.AddDays(target - start);
}
}
public static class DateTimeHelpers
{
public static DateTime Next(this DateTime from, DayOfWeek dayOfWeek)
{
int start = (int)from.DayOfWeek;
int target = (int)dayOfWeek;
if (target <= start)
{
target += 7;
}
return from.AddDays(target - start);
}
public static List<DateTime> NextWeeksDates() =>
Enumerable.Range(0, 7).Select(index =>
DateTime.Now.Next(DayOfWeek.Sunday).AddDays(index)).ToList();
public static List<DateTime> GetMonthDays(int month)
{
var year = DateTime.Now.Year;
return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
.Select(day => new DateTime(year, month, day))
.ToList();
}
}
var nextWeeksDates = DateTimeHelpers.NextWeeksDates();
var days = DateTimeHelpers.GetMonthDays(DateTime.Now.Month);
int year = 2025;
int month = 3;
List<List<DateOnly>> weeks = DateTimeHelpers.GetWeeksInMonth(year, month);
foreach (var (index, week) in weeks.Index())
{
Console.WriteLine($"Week {index + 1}: {string.Join(", ", week)}");
}

Comments are disabled for this gist.