Viyi.Util/DateTimes/DateTimeExtensions.cs

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

namespace Viyi.Util.DateTimes;

/// <summary>
/// 日期和时间扩展方法工具类
/// </summary>
/// <example>
/// <code>
/// string s1 = DateTime.Now.Format();
/// string s2 = DateTime.Now.FormatTime();
/// string s3 = DateTime.Today.FormatDate();
/// string s4 = DateTime.Now.FormatRfc3339();
/// string s5 = DateTime.Now.FormatRfc3339(DateTimeKind.Utc);
/// </code>
/// </example>
public static class DateTimeExtensions {
    private static IFormats Formats => DateTimeHelper.Formats;
    private static IFormats DefaultFormats => DateTimeHelper.DefaultFormats;

    /// <summary>
    /// 计算从1970年1月1日到指定日期的毫秒数数,与 Java 中 <c>java.util.Date.getTime()</c> 返回的值相同,即 UnixTime。
    /// </summary>
    /// <param name="value"></param>
    /// <param name="supplementTimeZone"></param>
    /// <param name="forceTimeZone"></param>
    /// <returns></returns>
    public static long ToJavaMilliseconds(
        this DateTime value,
        TimeZoneInfo? supplementTimeZone = null,
        bool forceTimeZone = false) {
        return ToUnixTimeMilliseconds(value, supplementTimeZone, forceTimeZone);
    }

    /// <summary>
    /// Returns the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z
    /// </summary>
    public static long ToUnixTimeMilliseconds(
        this DateTime value,
        TimeZoneInfo? supplementTimeZone = null,
        bool forceTimeZone = false) {
        return value
            .ToDateTimeOffset(supplementTimeZone, forceTimeZone)
            .ToUniversalTime()
            .ToUnixTimeMilliseconds();
    }

    /// <summary>
    /// Returns the number of seconds that have elapsed since 1970-01-01T00:00:00Z
    /// </summary>
    public static long ToUnixTimeSeconds(
        this DateTime value,
        TimeZoneInfo? supplementTimeZone = null,
        bool forceTimeZone = false) {
        return value
            .ToDateTimeOffset(supplementTimeZone, forceTimeZone)
            .ToUniversalTime()
            .ToUnixTimeSeconds();
    }

    /// <summary>
    /// 把 DateTime 转换成 DateTimeOffset
后面还有 82 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。