Show / Hide Table of Contents

    Class DateTimeExtensions

    DateTime 扩展方法

    Inheritance
    Object
    DateTimeExtensions
    Inherited Members
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Object.ReferenceEquals(Object, Object)
    Object.ToString()
    Namespace: Lett.Extensions
    Assembly: Lett.Extensions.dll
    Syntax
    public static class DateTimeExtensions

    Methods

    | Improve this Doc View Source

    EndOfDay(DateTime)

    获取 DateTime 的结束 (year-month-day 23:59:59.999)

    Declaration
    public static DateTime EndOfDay(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    DateTime

    返回一个新的DateTime

    Examples
    var dt = new DateTime(2019, 4, 1, 1, 2, 3);
    var rs = dt.EndOfDay(); // rs == new DateTime(2019, 4, 1, 23, 59, 59, 999);
    Exceptions
    Type Condition
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    EndOfMonth(DateTime)

    获取月的结束 DateTime (year-month-day 23:59:59.999)

    Declaration
    public static DateTime EndOfMonth(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    DateTime
    Examples
    var dt = new DateTime(2019, 4, 12, 1, 2, 3);
    var rs = dt.EndOfMonth(); // 2019-04-30 23:59:59.999
    Exceptions
    Type Condition
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    EndOfWeek(DateTime, DayOfWeek)

    获取本周结束的 DateTime (year-month-day 23:59:59.999)

    Declaration
    public static DateTime EndOfWeek(this DateTime this, DayOfWeek startOfWeek)
    Parameters
    Type Name Description
    DateTime this
    DayOfWeek startOfWeek

    设置每周的起始

    Returns
    Type Description
    DateTime

    返回一个新的DateTime

    Examples
    var dt2 = new DateTime(2019, 4, 1, 1, 2, 3);
    var rs2 = dt.EndOfWeek(DayOfWeek.Friday); // 2019-04-04 23:59:59.999
    Exceptions
    Type Condition
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    EndOfYear(DateTime)

    获取年的结束 DateTime (year-month-day 23:59:59.999)

    Declaration
    public static DateTime EndOfYear(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    DateTime
    Examples
    var dt = new DateTime(2019, 4, 12, 1, 2, 3);
    var rs = dt.EndOfYear(); // 2019-12-31 23.59.59.999
    Exceptions
    Type Condition
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    GetTotalDays(DateTime)

    获取时间戳的日表示

    Declaration
    public static double GetTotalDays(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    Double
    Examples
    var dt = new DateTime(2019, 4, 1, 11, 11, 11);
    dt.GetTotalDays(); // 17987.466099537036
    | Improve this Doc View Source

    GetTotalHours(DateTime)

    获取时间戳的小时表示

    Declaration
    public static double GetTotalHours(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    Double
    Examples
    var dt = new DateTime(2019, 4, 1, 11, 11, 11);
    dt.GetTotalHours(); // 431699.18638888886
    | Improve this Doc View Source

    GetTotalMilliseconds(DateTime)

    获取时间戳的毫秒表示

    Declaration
    public static double GetTotalMilliseconds(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    Double
    Examples
    var dt = new DateTime(2019, 4, 1, 11, 11, 11);
    dt.GetTotalMilliseconds(); // 1554117071000
    | Improve this Doc View Source

    GetTotalMinutes(DateTime)

    获取时间戳的分钟表示

    Declaration
    public static double GetTotalMinutes(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    Double
    Examples
    var dt = new DateTime(2019, 4, 1, 11, 11, 11);
    dt.GetTotalMinutes(); // 25901951.183333334
    | Improve this Doc View Source

    GetTotalSeconds(DateTime)

    获取时间戳的秒表示

    Declaration
    public static double GetTotalSeconds(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    Double
    Examples
    var dt = new DateTime(2019, 4, 1, 11, 11, 11);
    dt.GetTotalSeconds(); // 1554117071
    | Improve this Doc View Source

    IsBetween(DateTime, DateTime, DateTime)

    是否在指定日期之间

    Declaration
    public static bool IsBetween(this DateTime this, DateTime startDateTime, DateTime endDateTime)
    Parameters
    Type Name Description
    DateTime this
    DateTime startDateTime

    开始时间

    DateTime endDateTime

    结束时间

    Returns
    Type Description
    Boolean
    Examples
    var dt = new DateTime(2019,4,1);
    var startDate = new DateTime(2019,4,1,0,0,1);
    var endDate = new DateTime(2019,5,1);
    dt.IsBetween(startDateTime, endDateTime);  // false
    | Improve this Doc View Source

    IsToday(DateTime)

    是否当天

    Declaration
    public static bool IsToday(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    Boolean
    Examples
    var dt = DateTime.Now;
    dt.IsToday(); // true
    var dt2 = new DateTime(2019,1,1);
    dt2.IsToday(); // false
    | Improve this Doc View Source

    IsWeekDay(DateTime)

    是否工作日 (周一至周五)

    Declaration
    public static bool IsWeekDay(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    Boolean
    Examples
    var dt = new DateTime(2019,5,4);
    dt.IsWeekDay();  // false
    var dt2 = new DateTime(2019,5,1);
    dt2.IsWeekDay(); // true
    | Improve this Doc View Source

    IsZeroTime(DateTime)

    是否 0 时 0 分 0 秒

    Declaration
    public static bool IsZeroTime(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    Boolean
    Examples
    var dt = new DateTime(2019, 5, 4, 1, 1, 1);
    dt.IsZeroTime(); // false
    var dt = new DateTime(2019, 5, 4);
    dt.IsZeroTime(); // true
    | Improve this Doc View Source

    SetTime(DateTime, Int32, Int32, Int32)

    设置DateTime的time部分

    Declaration
    public static DateTime SetTime(this DateTime this, int hour, int minute, int second)
    Parameters
    Type Name Description
    DateTime this
    Int32 hour

    小时 (0-23)

    Int32 minute

    分钟 (0-59)

    Int32 second

    秒 (0-59)

    Returns
    Type Description
    DateTime

    返回一个新的DateTime

    Examples
    var dt2 = new DateTime(2019, 4, 1, 1, 2, 3);
    var rs2 = dt2.SetTime(23, 11, 11, 999);
    Exceptions
    Type Condition
    ArgumentOutOfRangeException

    hour、minute 和 second 参数描述了一个无法表示的日期时间

    | Improve this Doc View Source

    SetTime(DateTime, Int32, Int32, Int32, Int32)

    设置DateTime的time部分

    Declaration
    public static DateTime SetTime(this DateTime this, int hour, int minute, int second, int millisecond)
    Parameters
    Type Name Description
    DateTime this
    Int32 hour

    小时 (0-23)

    Int32 minute

    分钟 (0-59)

    Int32 second

    秒 (0-59)

    Int32 millisecond

    毫秒 (0-999) 默认0

    Returns
    Type Description
    DateTime

    返回一个新的DateTime

    Examples
    var dt2 = new DateTime(2019, 4, 1, 1, 2, 3);
    var rs2 = dt2.SetTime(23, 11, 11, 999);
    Exceptions
    Type Condition
    ArgumentOutOfRangeException

    hour、minute、second、millisecond 参数描述了一个无法表示的日期时间

    ArgumentOutOfRangeException

    millisecond 的有效值不在0和999之间

    | Improve this Doc View Source

    StartOfDay(DateTime)

    获取 DateTime 的开始 (year-month-day 00:00:00.000)

    Declaration
    public static DateTime StartOfDay(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    DateTime

    返回一个新的DateTime

    Examples
    var dt = new DateTime(2019, 4, 1, 21, 11, 11, 123);
    var rs = dt.StartOfDay(); // rs == new DateTime(2019, 4, 1, 0, 0, 0, 000)
    Exceptions
    Type Condition
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    StartOfMonth(DateTime)

    获取月的开始 DateTime (year-month-day 00:00:00.000)

    Declaration
    public static DateTime StartOfMonth(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    DateTime
    Examples
    var dt = new DateTime(2019, 4, 12, 1, 2, 3);
    var rs = dt.StartOfMonth(); // 2019-04-01 00:00:00.000
    Exceptions
    Type Condition
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    StartOfWeek(DateTime, DayOfWeek)

    获取本周开始 DateTime (year-month-day 00:00:00.000)

    Declaration
    public static DateTime StartOfWeek(this DateTime this, DayOfWeek startOfWeek)
    Parameters
    Type Name Description
    DateTime this
    DayOfWeek startOfWeek

    设置每周的起始

    Returns
    Type Description
    DateTime

    返回一个新的DateTime

    Examples
    var dt = new DateTime(2019, 4, 1, 1, 2, 3);
    var rs = dt.StartOfWeek(DayOfWeek.Sunday); // 2019-03-31 00:00:00
    Exceptions
    Type Condition
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    StartOfYear(DateTime)

    获取年的开始 DateTime (year-month-day 00:00:00.000)

    Declaration
    public static DateTime StartOfYear(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    DateTime
    Examples
    var dt = new DateTime(2019, 4, 12, 1, 2, 3);
    var rs = dt.StartOfYear(); // 2019-01-01 00:00:00.000
    Exceptions
    Type Condition
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    Tomorrow(DateTime)

    获取明天的 DateTime

    Declaration
    public static DateTime Tomorrow(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    DateTime
    Examples
    var dt = new DateTime(2019, 5, 4);
    var rs = dt.Tomorrow(); // new DateTime(2019, 5, 5);
    var dt2 = DateTime.MaxValue; // error -> throw ArgumentOutOfRangeException
    Exceptions
    Type Condition
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    ToString_Base(DateTime)

    格式化为 yyyy-MM-dd HH:mm:ss

    Declaration
    public static string ToString_Base(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    String
    Examples
    var dt = new DateTime(2019, 4, 1, 21, 11, 11, 123);
    dt.ToString_Base(); // "2019-04-01 21:11:11"
    Exceptions
    Type Condition
    FormatException
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    ToString_Day(DateTime)

    格式化为 yyyy-MM-dd

    Declaration
    public static string ToString_Day(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    String
    Examples
    var dt = new DateTime(2019, 4, 1, 21, 11, 11, 123);
    dt.ToString_Day(); // "2019-04-01"
    Exceptions
    Type Condition
    FormatException
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    ToString_Full(DateTime)

    格式化为 yyyy-MM-dd HH:mm:ss.fffffff

    Declaration
    public static string ToString_Full(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    String
    Examples
    var dt = new DateTime(2019, 4, 1, 21, 11, 11, 123);
    dt.ToString_Full(); // "2019-04-01 21:11:11.1230000"
    Exceptions
    Type Condition
    FormatException
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    ToString_Month(DateTime)

    格式化为 yyyy-MM

    Declaration
    public static string ToString_Month(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    String
    Examples
    var dt = new DateTime(2019, 4, 1, 21, 11, 11, 123);
    dt.ToString_Month(); // "2019-04"
    Exceptions
    Type Condition
    FormatException
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    ToString_ShortDay(DateTime)

    格式化为 yy-M-d

    Declaration
    public static string ToString_ShortDay(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    String
    Examples
    var dt = new DateTime(2019, 4, 1, 21, 11, 11, 123);
    dt.ToString_ShortDay(); // "19-4-1"
    Exceptions
    Type Condition
    FormatException
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    ToString_ShortMonth(DateTime)

    格式化为 yy-M

    Declaration
    public static string ToString_ShortMonth(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    String
    Examples
    var dt = new DateTime(2019, 4, 1, 21, 11, 11, 123);
    dt.ToString_ShortMonth(); // "19-4"
    Exceptions
    Type Condition
    FormatException
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    ToString_ShortTime(DateTime)

    格式化为 hh:mm:ss tt eg. 11:23:02 PM

    Declaration
    public static string ToString_ShortTime(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    String
    Examples
    var dt = new DateTime(2019, 4, 1, 21, 11, 11, 123);
    dt.ToString_ShortTime(); // "09:11:11 PM"
    Exceptions
    Type Condition
    FormatException
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    ToString_ShortYear(DateTime)

    格式化为 yy

    Declaration
    public static string ToString_ShortYear(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    String
    Examples
    var dt = new DateTime(2019, 4, 1, 21, 11, 11, 123);
    dt.ToString_ShortYear(); // "19"
    Exceptions
    Type Condition
    FormatException
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    ToString_Time(DateTime)

    格式化为 HH:mm:ss

    Declaration
    public static string ToString_Time(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    String
    Examples
    var dt = new DateTime(2019, 4, 1, 21, 11, 11, 123);
    dt.ToString_Time(); // "21:11:11"
    Exceptions
    Type Condition
    FormatException
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    ToString_Year(DateTime)

    格式化为 yyyy

    Declaration
    public static string ToString_Year(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    String
    Examples
    var dt = new DateTime(2019, 4, 1, 21, 11, 11, 123);
    dt.ToString_Year(); // "2019"
    Exceptions
    Type Condition
    FormatException
    ArgumentOutOfRangeException
    | Improve this Doc View Source

    Yesterday(DateTime)

    获取昨天的 DateTime

    Declaration
    public static DateTime Yesterday(this DateTime this)
    Parameters
    Type Name Description
    DateTime this
    Returns
    Type Description
    DateTime
    Examples
    var dt = new DateTime(2019, 5, 4, 1, 1, 1);
    var rs = dt.Yesterday(); // new DateTime(2019, 5, 3, 1, 1, 1)
    var dt2 = DateTime.MinValue; // error -> throw ArgumentOutOfRangeException
    Exceptions
    Type Condition
    ArgumentOutOfRangeException
    • Improve this Doc
    • View Source
    Back to top Copyright (c) 2019 viacooky. All rights reserved.