재우니의 블로그

 

여기 제공된 DateTimeLibrary의 메소드를 사용하는 예제를 각 메소드별로 구체적으로 설명하고 샘플 코드를 추가했습니다. 이 라이브러리를 사용하는 데 있어 개발자들에게 도움을 주기 위해 작성되었습니다. 각 메소드의 설명과 예시를 통해 라이브러리의 기능을 명확히 이해할 수 있을 것입니다.

 

 

1. FirstDayOfCurrentMonth 메소드 사용 예시

설명:

현재 달의 첫 번째 날을 반환합니다. 예를 들어, 현재가 2024년 11월이라면 "2024-11-01"을 반환합니다.

사용 예시:

using System;

namespace DateTimeLibraryUsage
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstDay = DateTimeLibrary.FirstDayOfCurrentMonth();
            Console.WriteLine($"이번 달의 첫 날은: {firstDay}");
        }
    }
}

출력:

이번 달의 첫 날은: 2024-11-01

2. FirstDayOfCurrentQuarter 메소드 사용 예시

설명:

현재 분기의 첫 번째 날을 반환합니다. 예를 들어, 현재가 2024년 11월이라면 해당 분기의 시작일인 "2024-10-01"을 반환합니다.

사용 예시:

using System;

namespace DateTimeLibraryUsage
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstDayOfQuarter = DateTimeLibrary.FirstDayOfCurrentQuarter();
            Console.WriteLine($"이번 분기의 첫 날은: {firstDayOfQuarter}");
        }
    }
}

출력:

이번 분기의 첫 날은: 2024-10-01

3. FirstDayOfCurrentWeek 메소드 사용 예시

설명:

현재 주의 첫 번째 날(월요일)을 반환합니다. 현재가 수요일이라면 이번 주 월요일의 날짜를 반환합니다.

사용 예시:

using System;

namespace DateTimeLibraryUsage
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstDayOfWeek = DateTimeLibrary.FirstDayOfCurrentWeek();
            Console.WriteLine($"이번 주의 첫 날은: {firstDayOfWeek}");
        }
    }
}

출력:

이번 주의 첫 날은: 2024-11-11

4. FirstDayOfCurrentYear 메소드 사용 예시

설명:

현재 연도의 첫 번째 날을 반환합니다. 예를 들어, "2024-01-01"과 같은 형식으로 반환합니다.

사용 예시:

using System;

namespace DateTimeLibraryUsage
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstDayOfYear = DateTimeLibrary.FirstDayOfCurrentYear();
            Console.WriteLine($"올해의 첫 날은: {firstDayOfYear}");
        }
    }
}

출력:

올해의 첫 날은: 2024-01-01

5. FirstDayOfLastMonth 메소드 사용 예시

설명:

지난 달의 첫 번째 날을 반환합니다. 예를 들어, 현재가 11월이면 "2024-10-01"을 반환합니다.

사용 예시:

using System;

namespace DateTimeLibraryUsage
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstDayOfLastMonth = DateTimeLibrary.FirstDayOfLastMonth();
            Console.WriteLine($"지난 달의 첫 날은: {firstDayOfLastMonth}");
        }
    }
}

출력:

지난 달의 첫 날은: 2024-10-01

6. LastDayOfCurrentMonth 메소드 사용 예시

설명:

현재 달의 마지막 날을 반환합니다. 예를 들어, 현재가 11월이라면 "2024-11-30"을 반환합니다.

사용 예시:

using System;

namespace DateTimeLibraryUsage
{
    class Program
    {
        static void Main(string[] args)
        {
            string lastDayOfCurrentMonth = DateTimeLibrary.LastDayOfCurrentMonth();
            Console.WriteLine($"이번 달의 마지막 날은: {lastDayOfCurrentMonth}");
        }
    }
}

출력:

이번 달의 마지막 날은: 2024-11-30

7. LastDayOfCurrentQuarter 메소드 사용 예시

설명:

현재 분기의 마지막 날을 반환합니다. 예를 들어, 현재가 11월이라면 해당 분기의 마지막 날인 "2024-12-31"을 반환합니다.

사용 예시:

using System;

namespace DateTimeLibraryUsage
{
    class Program
    {
        static void Main(string[] args)
        {
            string lastDayOfQuarter = DateTimeLibrary.LastDayOfCurrentQuarter();
            Console.WriteLine($"이번 분기의 마지막 날은: {lastDayOfQuarter}");
        }
    }
}

출력:

이번 분기의 마지막 날은: 2024-12-31

8. LastDayOfCurrentWeek 메소드 사용 예시

설명:

현재 주의 마지막 날(일요일)을 반환합니다.

사용 예시:

using System;

namespace DateTimeLibraryUsage
{
    class Program
    {
        static void Main(string[] args)
        {
            string lastDayOfWeek = DateTimeLibrary.LastDayOfCurrentWeek();
            Console.WriteLine($"이번 주의 마지막 날은: {lastDayOfWeek}");
        }
    }
}

출력:

이번 주의 마지막 날은: 2024-11-17

 

 

 

위에서 DateTimeLibrary를 활용하여 각 날짜 계산 메소드를 사용하는 방법을 명확하게 설명하고 있습니다. 이 샘플을 바탕으로 실제 프로젝트에서 각 날짜를 추출하고 활용하는 과정을 이해할 수 있을 것입니다. 

 

 

 


 

전체코드

namespace DateTimeLibrary
{
    using System;

    public static class DateTimeLibrary
    {
        /// <summary>
        /// Gets the first day of the current month.
        /// </summary>
        /// <returns>First day of the current month as a string.</returns>
        public static string FirstDayOfCurrentMonth()
        {
            DateTime today = DateTime.Today;
            DateTime firstDay = new DateTime(today.Year, today.Month, 1);
            return firstDay.ToShortDateString();
        }

        /// <summary>
        /// Gets the first day of the current quarter.
        /// </summary>
        /// <returns>First day of the current quarter as a string.</returns>
        public static string FirstDayOfCurrentQuarter()
        {
            DateTime today = DateTime.Today;
            int quarterStartMonth = ((today.Month - 1) / 3) * 3 + 1;
            DateTime firstDayOfQuarter = new DateTime(today.Year, quarterStartMonth, 1);
            return firstDayOfQuarter.ToShortDateString();
        }

        /// <summary>
        /// Gets the first day of the current week, assuming the week starts on Monday.
        /// </summary>
        /// <returns>First day of the current week as a string.</returns>
        public static string FirstDayOfCurrentWeek()
        {
            DateTime today = DateTime.Today;
            int daysToSubtract = (int)today.DayOfWeek == 0 ? 6 : (int)today.DayOfWeek - 1;
            DateTime firstDayOfWeek = today.AddDays(-daysToSubtract);
            return firstDayOfWeek.ToShortDateString();
        }

        /// <summary>
        /// Gets the first day of the current year.
        /// </summary>
        /// <returns>First day of the current year as a string.</returns>
        public static string FirstDayOfCurrentYear()
        {
            DateTime today = DateTime.Today;
            DateTime firstDayOfYear = new DateTime(today.Year, 1, 1);
            return firstDayOfYear.ToShortDateString();
        }

        /// <summary>
        /// Gets the first day of the last month.
        /// </summary>
        /// <returns>First day of the last month as a string.</returns>
        public static string FirstDayOfLastMonth()
        {
            DateTime today = DateTime.Today.AddMonths(-1);
            DateTime firstDayOfLastMonth = new DateTime(today.Year, today.Month, 1);
            return firstDayOfLastMonth.ToShortDateString();
        }

        /// <summary>
        /// Gets the first day of the last quarter.
        /// </summary>
        /// <returns>First day of the last quarter as a string.</returns>
        public static string FirstDayOfLastQuarter()
        {
            DateTime today = DateTime.Today.AddMonths(-3);
            int quarterStartMonth = ((today.Month - 1) / 3) * 3 + 1;
            DateTime firstDayOfLastQuarter = new DateTime(today.Year, quarterStartMonth, 1);
            return firstDayOfLastQuarter.ToShortDateString();
        }

        /// <summary>
        /// Gets the first day of the last week, assuming the week starts on Monday.
        /// </summary>
        /// <returns>First day of the last week as a string.</returns>
        public static string FirstDayOfLastWeek()
        {
            DateTime today = DateTime.Today.AddDays(-7);
            int daysToSubtract = (int)today.DayOfWeek == 0 ? 6 : (int)today.DayOfWeek - 1;
            DateTime firstDayOfLastWeek = today.AddDays(-daysToSubtract);
            return firstDayOfLastWeek.ToShortDateString();
        }

        /// <summary>
        /// Gets the first day of the last year.
        /// </summary>
        /// <returns>First day of the last year as a string.</returns>
        public static string FirstDayOfLastYear()
        {
            DateTime today = DateTime.Today;
            DateTime firstDayOfLastYear = new DateTime(today.Year - 1, 1, 1);
            return firstDayOfLastYear.ToShortDateString();
        }

        /// <summary>
        /// Gets the first day of the next month.
        /// </summary>
        /// <returns>First day of the next month as a string.</returns>
        public static string FirstDayOfNextMonth()
        {
            DateTime today = DateTime.Today.AddMonths(1);
            DateTime firstDayOfNextMonth = new DateTime(today.Year, today.Month, 1);
            return firstDayOfNextMonth.ToShortDateString();
        }

        /// <summary>
        /// Gets the first day of the next quarter.
        /// </summary>
        /// <returns>First day of the next quarter as a string.</returns>
        public static string FirstDayOfNextQuarter()
        {
            DateTime today = DateTime.Today;
            int quarterStartMonth = ((today.Month - 1) / 3) * 3 + 4;
            if (quarterStartMonth > 12)
            {
                quarterStartMonth -= 12;
                today = today.AddYears(1);
            }
            DateTime firstDayOfNextQuarter = new DateTime(today.Year, quarterStartMonth, 1);
            return firstDayOfNextQuarter.ToShortDateString();
        }

        /// <summary>
        /// Gets the first day of the next week, assuming the week starts on Monday.
        /// </summary>
        /// <returns>First day of the next week as a string.</returns>
        public static string FirstDayOfNextWeek()
        {
            DateTime today = DateTime.Today;
            int daysToAdd = (int)today.DayOfWeek == 0 ? 1 : 8 - (int)today.DayOfWeek;
            DateTime firstDayOfNextWeek = today.AddDays(daysToAdd);
            return firstDayOfNextWeek.ToShortDateString();
        }

        /// <summary>
        /// Gets the first day of the next year.
        /// </summary>
        /// <returns>First day of the next year as a string.</returns>
        public static string FirstDayOfNextYear()
        {
            DateTime today = DateTime.Today;
            DateTime firstDayOfNextYear = new DateTime(today.Year + 1, 1, 1);
            return firstDayOfNextYear.ToShortDateString();
        }

        /// <summary>
        /// Gets the last day of the current month.
        /// </summary>
        /// <returns>Last day of the current month as a string.</returns>
        public static string LastDayOfCurrentMonth()
        {
            DateTime today = DateTime.Today;
            DateTime lastDay = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
            return lastDay.ToShortDateString();
        }

        /// <summary>
        /// Gets the last day of the current quarter.
        /// </summary>
        /// <returns>Last day of the current quarter as a string.</returns>
        public static string LastDayOfCurrentQuarter()
        {
            DateTime today = DateTime.Today;
            int quarterEndMonth = ((today.Month - 1) / 3) * 3 + 3;
            DateTime lastDayOfQuarter = new DateTime(today.Year, quarterEndMonth, DateTime.DaysInMonth(today.Year, quarterEndMonth));
            return lastDayOfQuarter.ToShortDateString();
        }

        /// <summary>
        /// Gets the last day of the current week, assuming the week ends on Sunday.
        /// </summary>
        /// <returns>Last day of the current week as a string.</returns>
        public static string LastDayOfCurrentWeek()
        {
            DateTime today = DateTime.Today;
            int daysToAdd = (int)today.DayOfWeek == 0 ? 0 : 7 - (int)today.DayOfWeek;
            DateTime lastDayOfWeek = today.AddDays(daysToAdd);
            return lastDayOfWeek.ToShortDateString();
        }

        /// <summary>
        /// Gets the last day of the current year.
        /// </summary>
        /// <returns>Last day of the current year as a string.</returns>
        public static string LastDayOfCurrentYear()
        {
            DateTime today = DateTime.Today;
            DateTime lastDayOfYear = new DateTime(today.Year, 12, 31);
            return lastDayOfYear.ToShortDateString();
        }

        /// <summary>
        /// Gets the last day of the last month.
        /// </summary>
        /// <returns>Last day of the last month as a string.</returns>
        public static string LastDayOfLastMonth()
        {
            DateTime today = DateTime.Today.AddMonths(-1);
            DateTime lastDayOfLastMonth = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
            return lastDayOfLastMonth.ToShortDateString();
        }

        /// <summary>
        /// Gets the last day of the last quarter.
        /// </summary>
        /// <returns>Last day of the last quarter as a string.</returns>
        public static string LastDayOfLastQuarter()
        {
            DateTime today = DateTime.Today.AddMonths(-3);
            int quarterEndMonth = ((today.Month - 1) / 3) * 3 + 3;
            DateTime lastDayOfLastQuarter = new DateTime(today.Year, quarterEndMonth, DateTime.DaysInMonth(today.Year, quarterEndMonth));
            return lastDayOfLastQuarter.ToShortDateString();
        }

        /// <summary>
        /// Gets the last day of the last year.
        /// </summary>
        /// <returns>Last day of the last year as a string.</returns>
        public static string LastDayOfLastYear()
        {
            DateTime today = DateTime.Today;
            DateTime lastDayOfLastYear = new DateTime(today.Year - 1, 12, 31);
            return lastDayOfLastYear.ToShortDateString();
        }

        /// <summary>
        /// Gets the last day of the next month.
        /// </summary>
        /// <returns>Last day of the next month as a string.</returns>
        public static string LastDayOfNextMonth()
        {
            DateTime today = DateTime.Today.AddMonths(1);
            DateTime lastDayOfNextMonth = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
            return lastDayOfNextMonth.ToShortDateString();
        }

        /// <summary>
        /// Gets the last day of the next quarter.
        /// </summary>
        /// <returns>Last day of the next quarter as a string.</returns>
        public static string LastDayOfNextQuarter()
        {
            DateTime today = DateTime.Today.AddMonths(3);
            int quarterEndMonth = ((today.Month - 1) / 3) * 3 + 3;
            if (quarterEndMonth > 12)
            {
                quarterEndMonth -= 12;
                today = today.AddYears(1);
            }
            DateTime lastDayOfNextQuarter = new DateTime(today.Year, quarterEndMonth, DateTime.DaysInMonth(today.Year, quarterEndMonth));
            return lastDayOfNextQuarter.ToShortDateString();
        }

        /// <summary>
        /// Gets the last day of the next week, assuming the week ends on Sunday.
        /// </summary>
        /// <returns>Last day of the next week as a string.</returns>
        public static string LastDayOfNextWeek()
        {
            DateTime today = DateTime.Today;
            int daysToAdd = (int)today.DayOfWeek == 0 ? 7 : 14 - (int)today.DayOfWeek;
            DateTime lastDayOfNextWeek = today.AddDays(daysToAdd);
            return lastDayOfNextWeek.ToShortDateString();
        }

        /// <summary>
        /// Gets the last day of the next year.
        /// </summary>
        /// <returns>Last day of the next year as a string.</returns>
        public static string LastDayOfNextYear()
        {
            DateTime today = DateTime.Today;
            DateTime lastDayOfNextYear = new DateTime(today.Year + 1, 12, 31);
            return lastDayOfNextYear.ToShortDateString();
        }
    }
}