DateTimeExtension.cs 1.2 KB

123456789101112131415161718192021222324252627282930
  1. namespace SharedKernel.Extensions
  2. {
  3. public static class DateTimeExtension
  4. {
  5. // 날짜 포맷 조회
  6. public static string GetDateAt(this DateTime dateTime, string format = "yyyy.MM.dd HH:mm:ss") => dateTime.ToLocalTime().ToString(format);
  7. public static string? GetDateAt(this DateTime? dateTime, string format = "yyyy.MM.dd HH:mm:ss") => dateTime?.ToLocalTime().ToString(format) ?? null;
  8. // 변경 시간 남은 기간 일 수 계산
  9. public static short GetChangeCycleDay(this DateTime? changedAt, ushort? cycleDay = 0)
  10. {
  11. if (cycleDay > 0 && cycleDay.HasValue)
  12. {
  13. if (changedAt.HasValue)
  14. {
  15. // 변경 가능 기간 계산 (현재 날짜 - 설정된 변경 기간)
  16. return (short)((cycleDay + 1) - (DateTime.UtcNow - changedAt.Value).TotalDays); // 남은 일수 계산
  17. }
  18. }
  19. return 0;
  20. }
  21. // 현재 시간으로 부터 지정된 일수 이상 지난 날짜인지 확인
  22. public static bool IsDateOverDue(this DateTime createdAt, ushort days)
  23. {
  24. return (DateTime.UtcNow - createdAt).TotalDays > days;
  25. }
  26. }
  27. }