DateTimeExtension.cs 1.1 KB

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