开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载Humanizer 是 .NET 生态中专注于字符串、枚举、日期与时间的人性化处理库。其中FluentDate流式日期子模块提供了一组「以自然语言方式构造日期」的 APIOn.June正是该模块中用于表示「当前年份六月某一天」的静态类。本文以 website/versioned_docs/version-2.14.1/api/Humanizer.On.June.md 为骨架结合源码与测试完整讲解该类 30 个静态属性与 1 个静态方法的用法、底层实现与适用场景。读完本文你将掌握如何在业务代码中直接写出On.June.The15th这类自解释性极强的日期表达式。On.June 类概览On.June是嵌套在On静态类内部的公开类位于Humanizer命名空间下用于提供六月的流式日期访问器fluent date accessors。public class On.June继承关系System.Object→On.June即直接继承System.Object无额外基类类修饰符public可被外部直接访问成员性质全部为public static成员无需实例化即可调用文档路径API 参考文档位于 website/versioned_docs/version-2.14.1/api/Humanizer.On.June.md当前主版本对应文档为 website/docs/api/Humanizer.On.June.md在 On.Days.cs 中On是public class OnJune是其内部嵌套类。整个On类共包含 12 个月份的嵌套类January 至 DecemberJune是其中之一。静态属性The1st 到 The30thOn.June提供 30 个静态只读属性分别对应六月的 1 日至 30 日。每个属性均返回System.DateTime类型表示当前年份的对应日期。public static System.DateTime The10th { get; }完整属性清单如下属性名返回日期属性名返回日期The1st当前年 6 月 1 日The16th当前年 6 月 16 日The2nd当前年 6 月 2 日The17th当前年 6 月 17 日The3rd当前年 6 月 3 日The18th当前年 6 月 18 日The4th当前年 6 月 4 日The19th当前年 6 月 19 日The5th当前年 6 月 5 日The20th当前年 6 月 20 日The6th当前年 6 月 6 日The21st当前年 6 月 21 日The7th当前年 6 月 7 日The22nd当前年 6 月 22 日The8th当前年 6 月 8 日The23rd当前年 6 月 23 日The9th当前年 6 月 9 日The24th当前年 6 月 24 日The10th当前年 6 月 10 日The25th当前年 6 月 25 日The11th当前年 6 月 11 日The26th当前年 6 月 26 日The12th当前年 6 月 12 日The27th当前年 6 月 27 日The13th当前年 6 月 13 日The28th当前年 6 月 28 日The14th当前年 6 月 14 日The29th当前年 6 月 29 日The15th当前年 6 月 15 日The30th当前年 6 月 30 日以源码实现为例The1st与The10th的实际定义如下On.Days.cs/// summary /// The 1st day of June of the current year /// /summary public static DateTime The1st new(DateTime.Now.Year, 6, 1); /// summary /// The 10th day of June of the current year /// /summary public static DateTime The10th new(DateTime.Now.Year, 6, 10);属性命名与序数词规则属性命名遵循英文序数词惯例1、2、3 对应The1st、The2nd、The3rd21、22、23 对应The21st、The22nd、The23rd其余日期统一使用The4th至The30th形式。这一命名由 On.Days.tt 模板在生成时通过day.Ordinalize()扩展方法自动产生无需手工维护也保证了 12 个月份的命名风格完全一致。静态方法The(int dayNumber)除 30 个固定属性外On.June还提供一个通用方法用于访问任意日期只要该日期在六月范围内public static System.DateTime The(int dayNumber);参数dayNumber类型System.Int32表示希望获取的六月中的第几天返回值System.DateTime即当前年份 6 月dayNumber日的日期源码实现On.Days.cspublic static DateTime The(int dayNumber) new(DateTime.Now.Year, 6, dayNumber);典型调用示例// 获取当前年份 6 月 15 日 var midJune On.June.The(15); // 与属性形式等价 var sameDate On.June.The15th;参数边界说明由于The(int)直接以dayNumber作为DateTime构造函数的日参数当传入值超出 6 月的合法天数范围如0、31或负数时System.DateTime构造函数会抛出ArgumentOutOfRangeException。从源码结构可以推断这是The(int)与固定属性之间的关键差异——固定属性在生成时已按 6 月实际天数30 天预先校验而动态方法要求调用方自行保证dayNumber处于1到30之间。此外dayNumber为int类型而非枚举或校验型类型因此建议在传入业务数据前显式校验。底层实现T4 模板驱动的代码生成On.June以及On类中其余 11 个月份嵌套类并非手写代码而是由 T4 文本模板 On.Days.tt 自动生成。模板核心逻辑如下const int leapYear 2012; for (var month 1; month 12; month) { var firstDayOfMonth new DateTime(leapYear, month, 1); var monthName firstDayOfMonth.ToString(MMMM); // 生成 The(int dayNumber) 方法 public static DateTime The(int dayNumber) new(DateTime.Now.Year, # month #, dayNumber); for (var day 1; day DateTime.DaysInMonth(leapYear, month); day) { var ordinalDay day.Ordinalize(); // 生成 The1st / The2nd / ... 静态属性 public static DateTime The# ordinalDay # new(DateTime.Now.Year, # month #, # day #); } }几个值得注意的实现细节闰年锚点模板以 2012 年闰年为基准计算每月天数因此 2 月会生成 29 个属性The1st至The29th其余月份按其自然天数生成。6 月固定生成 30 个属性与文档中的属性清单完全一致。序数词生成属性名中的1st、2nd、3rd等后缀由 Humanizer 自身的Ordinalize()扩展方法产出这恰好是 Humanizer「自我使用」的一个实例。月份名称通过firstDayOfMonth.ToString(MMMM)得到本地化的完整月份名January、February 等用于类名与 XML 注释。年份来源所有属性与方法统一使用DateTime.Now.Year即「当前年份」。这意味着属性值会随系统当前年份的变化而动态改变而非固定常量。与 In.June / In.JuneOf 的关系和区别在 Humanizer 的 FluentDate 模块中On与In是两个互补的入口In.Months.cs 提供了月份粒度的访问器/// summary /// Returns 1st of June of the current year /// /summary public static DateTime June new(DateTime.UtcNow.Year, 6, 1); /// summary /// Returns 1st of June of the year passed in /// /summary public static DateTime JuneOf(int year) new(year, 6, 1);两者对比如下维度On.JuneIn.June/In.JuneOf(int)定位粒度日1 日至 30 日月仅当月 1 日年份来源DateTime.Now.Year当前年份June用当前年份JuneOf(year)用指定年份返回类型System.DateTimeSystem.DateTime典型用途表达「某年六月的某一天」表达「某年六月的第一天」实际使用中On.June.The5th与In.June的语义层级不同前者精确到日后者仅到月首。若需要指定非当前年份的六月某日从源码结构看On.June系列并未提供年份参数版本需要自行组合如new DateTime(2025, 6, 15)或配合In.JuneOf思路手写构造函数。测试验证与使用示例Humanizer 仓库通过 tests/Humanizer.Tests/FluentDate/OnTests.cs 对On系列访问器进行了验证模式为「断言属性值与DateTime构造函数等价」[Fact] public void OnJanuaryThe23rd() Assert.Equal(new(DateTime.Now.Year, 1, 23), On.January.The23rd); [Fact] public void OnDecemberThe4th() Assert.Equal(new(DateTime.Now.Year, 12, 4), On.December.The4th); [Fact] public void OnFebruaryThe() Assert.Equal(new(DateTime.Now.Year, 2, 11), On.February.The(11));In.June的对应测试位于 InTests.cs[Fact] public void InJune() Assert.Equal(new(DateTime.Now.Year, 6, 1), In.June); [Fact] public void InJuneOf2009() Assert.Equal(new(2009, 6, 1), In.JuneOf(2009));此外GeneratedFluentDateTests.cs 中包含(June, 6)的月份序号映射数据用于对生成代码做系统性校验确保每个月份的类名、月份号与日期属性数量一致。实际业务场景On.June最适合在以下场景中提升代码可读性using Humanizer; // 场景一报表中固定引用「每年六月的结算日」 var settlementDay On.June.The30th; // 场景二年中节点判断当前年份六月下旬 if (DateTime.Now On.June.The15th) { // 进入下半年预算周期 } // 场景三动态天数如用户选择的日期 var selected On.June.The(userInputDay);这类表达式的优势在于On.June.The15th相比new DateTime(DateTime.Now.Year, 6, 15)语义一目了然无需阅读构造函数参数即可理解「六月的第 15 天」且天然与「当前年份」绑定避免了手工拼装年份的样板代码。使用注意点年份语义所有On.June成员基于DateTime.Now.Year在跨年运行的长驻进程如后台服务中其返回值会随系统时钟变化不适合作为「冻结的固定日期常量」缓存。动态方法的范围校验The(int dayNumber)对越界值无内置保护传入1到30之外的值将触发ArgumentOutOfRangeException调用方需自行校验。时区因素On.June使用DateTime.Now本地时间取年份而In.June使用DateTime.UtcNowUTC 时间取年份两者在时区边界跨年瞬间可能产生年份差异从源码可确认这一细节差异。API 版本本文描述的On.June完整成员30 个属性 1 个方法对应仓库中version-2.14.1的 API 参考文档现行主分支文档 website/docs/api/Humanizer.On.June.md 内容与之保持一致。综上On.June是 Humanizer FluentDate 模块中「月份 日」粒度的流式日期入口通过代码生成保证了 12 个月份 API 的高度一致性适合在需要以自然语言风格构造「当前年份某月某日」的业务代码中直接采用。赞分享开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载相关推荐Humanizer 流式日期 API 详解On.November 类与 11 月日期访问器Humanizer 流式日期 API 详解On.November 类与 11 月日期访问器 本篇技术指南聚焦于 .NET Humanizer 库中 On.No开发工具Humanizer 流式日期 API 详解使用 On.June 类快速构造当年六月的任意日期Humanizer 流式日期 API 详解使用 On.June 类快速构造当年六月的任意日期 Humanizer 的 FluentDate 子命名空间提供了一开发工具Humanizer 流式日期 API 详解OnDate.February 与 2 月日期访问器Humanizer 流式日期 API 详解OnDate.February 与 2 月日期访问器 Humanizer 提供了一套以自然语言为核心的 .NET 字开发工具上一篇G-Helper深度解析华硕笔记本性能优化的完整方案下一篇NVIDIA Profile Inspector终极指南3步解锁显卡隐藏性能创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考