專案中頻繁需要:相對時間顯示(如「3分鐘前」)、兩日期差值計算、生日提醒、星期轉換等。本文整理一個功能完整的 DateTimeHelper 靜態類,涵蓋實務上最常見的時間運算場景,程式碼經重構優化,可直接投入生產環境。
using System;
namespace CLB.Utility.CharTools
{
public static class DateTimeHelper
{
///
/// 取得相對時間描述:剛剛、N秒前、N分鐘前、N小時前、N天前、N週前、1個月前、或具體日期
///
public static string GetRelativeTime(DateTime dt)
{
TimeSpan span = DateTime.Now - dt;
if (span.TotalDays > 60) return dt.ToString("yyyy/MM/dd");
if (span.TotalDays > 30) return "1個月前";
if (span.TotalDays > 14) return "2週前";
if (span.TotalDays > 7) return "1週前";
if (span.TotalDays > 1) return $"{(int)Math.Floor(span.TotalDays)}天前";
if (span.TotalHours > 1) return $"{(int)Math.Floor(span.TotalHours)}小時前";
if (span.TotalMinutes > 1) return $"{(int)Math.Floor(span.TotalMinutes)}分鐘前";
if (span.TotalSeconds >= 1) return $"{(int)Math.Floor(span.TotalSeconds)}秒前";
return "剛剛";
}
///
/// 計算兩日期差值:可選擇回傳總天數(含小數) 或 整數天數
///
public static string GetDayDiff(DateTime start, DateTime end, bool includeFraction)
{
TimeSpan ts = end - start;
return includeFraction ? ts.TotalDays.ToString("0.##") : ts.Days.ToString();
}
///
/// 計算兩日期詳細差值:X天X小時X分X秒
///
public static string GetDetailDiff(DateTime dt1, DateTime dt2)
{
TimeSpan ts = (dt1 > dt2) ? dt1 - dt2 : dt2 - dt1;
return $"{ts.Days}天{ts.Hours}小時{ts.Minutes}分{ts.Seconds}秒";
}
///
/// 英文星期轉中文:Sunday → 星期日
///
public static string GetChineseWeekDay(string englishWeek)
{
return englishWeek?.Trim() switch
{
"Sunday" => "星期日",
"Monday" => "星期一",
"Tuesday" => "星期二",
"Wednesday" => "星期三",
"Thursday" => "星期四",
"Friday" => "星期五",
"Saturday" => "星期六",
_ => englishWeek ?? ""
};
}
///
/// 判斷目標日期是否在基準日期 N 天內 (含當天)
///
public static bool IsWithinDays(string baseDateStr, string targetDateStr, int days)
{
if (!DateTime.TryParse(baseDateStr, out DateTime baseDate) ||
!DateTime.TryParse(targetDateStr, out DateTime targetDate))
return false;
return targetDate <= baseDate.AddDays(days);
}
///
/// 生日提醒:回傳「今天」、「本月N號」、「上月N號」、「下月N號」、「已過N月」、「M月D日」
///
public static string GetBirthdayReminder(DateTime birthday)
{
DateTime now = DateTime.Now;
int nowMonth = now.Month, birthMonth = birthday.Month;
int monthDiff = nowMonth - birthMonth;
// 跨年處理
if (nowMonth == 12 && birthMonth == 1) return $"下月{birthday.Day}號";
if (nowMonth == 1 && birthMonth == 12) return $"上月{birthday.Day}號";
if (monthDiff == 1) return $"上月{birthday.Day}號";
if (monthDiff == -1) return $"下月{birthday.Day}號";
if (monthDiff == 0)
{
if (now.Day == birthday.Day) return "今天";
return $"本月{birthday.Day}號";
}
if (monthDiff > 1) return $"已過{monthDiff}月";
return $"{birthday.Month}月{birthday.Day}日";
}
}
}
// 相對時間
Console.WriteLine(DateTimeHelper.GetRelativeTime(DateTime.Now.AddMinutes(-5))); // 5分鐘前
Console.WriteLine(DateTimeHelper.GetRelativeTime(DateTime.Now.AddDays(-10))); // 10天前
// 差值計算
DateTime d1 = new DateTime(2024,1,1);
DateTime d2 = new DateTime(2024,1,15,12,30,0);
Console.WriteLine(DateTimeHelper.GetDayDiff(d1, d2, true)); // 14.52
Console.WriteLine(DateTimeHelper.GetDayDiff(d1, d2, false)); // 14
Console.WriteLine(DateTimeHelper.GetDetailDiff(d1, d2)); // 14天12小時30分0秒
// 星期轉換
Console.WriteLine(DateTimeHelper.GetChineseWeekDay("Friday")); // 星期五
// 生日提醒
DateTime bd = new DateTime(1990, 12, 25);
Console.WriteLine(DateTimeHelper.GetBirthdayReminder(bd)); // 視今日日期而定
DateTimeOffset 或傳入 TimeZoneInfo。DateTime.Now 減少系統呼叫。