@@ -675,9 +675,23 @@ public static long GetWeekEndTimestamp()
675
675
/// 获取本月开始时间
676
676
/// </summary>
677
677
/// <returns>本月1号零点时间</returns>
678
+ /// <remarks>
679
+ /// 此方法基于UTC时间计算本月开始时间:
680
+ /// 1. 获取当前UTC时间的年份和月份
681
+ /// 2. 创建一个新的DateTime对象,设置为本月1号零点
682
+ /// 3. 返回的时间为UTC时区的时间
683
+ ///
684
+ /// 示例:
685
+ /// - 当前UTC时间为2024-01-15 14:30:00
686
+ /// - 返回时间为2024-01-01 00:00:00 (UTC)
687
+ ///
688
+ /// 注意:
689
+ /// - 返回的是UTC时区的时间,如需本地时间请使用TimeZoneInfo.ConvertTimeFromUtc转换
690
+ /// - 返回时间的Hour/Minute/Second/Millisecond均为0
691
+ /// </remarks>
678
692
public static DateTime GetMonthStartTime ( )
679
693
{
680
- return new DateTime ( DateTime . Now . Year , DateTime . Now . Month , 1 ) ;
694
+ return new DateTime ( DateTime . UtcNow . Year , DateTime . UtcNow . Month , 1 ) ;
681
695
}
682
696
683
697
/// <summary>
@@ -711,9 +725,23 @@ public static long GetMonthEndTimestamp()
711
725
/// 获取本年开始时间
712
726
/// </summary>
713
727
/// <returns>本年1月1日零点时间</returns>
728
+ /// <remarks>
729
+ /// 此方法基于UTC时间计算年份:
730
+ /// 1. 获取当前UTC时间的年份
731
+ /// 2. 返回该年份1月1日零点时间
732
+ ///
733
+ /// 示例:
734
+ /// - 当前UTC时间为2024-03-15 14:30:00
735
+ /// - 返回2024-01-01 00:00:00
736
+ ///
737
+ /// 注意:
738
+ /// - 返回的是UTC时间,不考虑本地时区
739
+ /// - 返回时间的时分秒毫秒都为0
740
+ /// - 使用DateTime.UtcNow避免时区转换带来的问题
741
+ /// </remarks>
714
742
public static DateTime GetYearStartTime ( )
715
743
{
716
- return new DateTime ( DateTime . Now . Year , 1 , 1 ) ;
744
+ return new DateTime ( DateTime . UtcNow . Year , 1 , 1 ) ;
717
745
}
718
746
719
747
/// <summary>
0 commit comments