VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之Monthly数据类型(2)

public void TestMethods() { Monthly plan = 201801; var tar = new DateTime(2018, 1, 1); var tip = false; //Dot Assert.AreEqual(new Monthly(0, 11), 11); Assert.AreEqual(new Monthly(1, 1), 101); Assert.AreEqual(new Monthly(100, 12), 10012); Assert.AreEqual(new Monthly(2018, 12), 201812); //Tickes Assert.AreEqual(((Monthly)101).Tickes, 13); Assert.AreEqual(((Monthly)201811).Tickes, 2018 * 12 + 11); //加月 Assert.AreEqual(plan.AddMonths(-1), 201712); Assert.AreEqual(plan.AddMonths(-23), 201602); Assert.AreEqual(plan.AddMonths(22), 201911); //加月(随机) for (int i = 0; i < 100; i++) { var rd = new Random(Guid.NewGuid().GetHashCode()).Next(100); Assert.AreEqual(plan.AddMonths(rd), Monthly.FromDate(tar.AddMonths(rd))); Assert.AreEqual(plan.AddMonths(rd).Dot, tar.AddMonths(rd).Year * 100 + tar.AddMonths(rd).Month); } //加年 Assert.IsTrue(plan.AddYears(6) == 202401); Assert.IsTrue(plan.AddYears(-18) == 200001); //加年(异常) try { var m = Monthly.Current.AddYears(-3000); } catch (Exception e) { if (e.Message.Contains("beteen 1 and 120000")) { tip = true; } } Assert.IsTrue(tip); //月份差 Assert.AreEqual(plan.SpanMonths(201711), 2); Assert.AreEqual(plan.SpanMonths(201902), -13); //比较大小 Assert.AreEqual(plan.CompareTo(201801), 0); Assert.AreEqual(plan.CompareTo(201701), 1); Assert.AreEqual(plan.CompareTo(202001), -1); //构造 Assert.AreEqual(Monthly.FromDot(3), 3); Assert.AreEqual(Monthly.FromTickes(13), 101); Assert.AreEqual(Monthly.FromDate(new DateTime(2018, 12, 12)), 201812); Assert.AreEqual(Monthly.FromString("2018/01"), 201801); Assert.AreEqual(Monthly.FromString("2018年01月"), 201801); Assert.AreEqual(Monthly.FromString("2018@01/01"), 201801); Assert.AreEqual(Monthly.FromString((new DateTime(2018, 1, 1)).ToString()), 201801); Assert.AreEqual(Monthly.FromString("3"), 3); //月份轴 var axis = Monthly.Axis(201711, 201901); Assert.IsTrue(axis.Count == 15); Assert.AreEqual(axis[0], 201711); Assert.AreEqual(axis[3], 201802); Assert.AreEqual(axis[14], 201901); axis = Monthly.Axis(201812, 201712); Assert.IsTrue(axis.Count == 13); Assert.AreEqual(axis[0], 201812); Assert.AreEqual(axis[12], 201712); //异常 tip = false; try { Monthly m = 201800; } catch (Exception e) { if (e.Message.Contains("correct dot format")) { tip = true; } } //dot format Assert.IsTrue(tip); tip = false; try { Monthly m = Monthly.FromDot(13); } catch (Exception e) { if (e.Message.Contains("correct dot format")) { tip = true; } } //13月 Assert.IsTrue(tip); tip = false; try { Monthly m = Monthly.FromTickes(999999); } catch (Exception e) { if (e.Message.Contains("must beteen 1 and 120000")) { tip = true; } } //越界 Assert.IsTrue(tip); tip = false; try { Monthly m = Monthly.FromString(null); } catch (Exception e) { if (e.Message.Contains("null or empty")) { tip = true; } } //IsNullOrEmpty Assert.IsTrue(tip); tip = false; try { Monthly m = Monthly.FromString("abc"); } catch (Exception e) { if (e.Message.Contains("parameters")) { tip = true; } } //格式错误 Assert.IsTrue(tip); tip = false; try { Monthly m = Monthly.FromString("88"); } catch (Exception e) { if (e.Message.Contains("must beteen")) { tip = true; } } //越界 Assert.IsTrue(tip); } [TestMethod] public void TestOps() { Monthly plan = 201801; var tar = Monthly.FromString("2018.01"); Assert.AreEqual(plan + 12, 201901); Assert.AreEqual(plan - 13, 201612); Assert.AreEqual(plan - (Monthly)201701, 12); Assert.AreEqual(plan - (new DateTime(2017, 12, 12)), 1); Assert.AreEqual(--plan, 201712); Assert.AreEqual(++plan, 201801); Assert.IsTrue(plan == Monthly.FromDot(201801)); Assert.IsTrue(plan != Monthly.FromDot(201802)); Assert.IsTrue(plan >= Monthly.FromDot(201801)); Assert.IsTrue(plan < Monthly.FromDot(201803)); } [TestMethod] public void TestOvr() { Monthly plan = 201801; var tar = Monthly.FromString("2018.01"); //哈希码(相同dot具有相同的哈希码) Assert.AreEqual(plan.GetHashCode(), tar.GetHashCode()); tar++; Assert.AreNotEqual(plan.GetHashCode(), tar.GetHashCode()); //格式化 Assert.AreEqual(plan.ToString(), "2018/01"); Assert.AreEqual(plan.ToString("yy/mm"), "18/01"); Assert.AreEqual(Monthly.FromDot(501).ToString("yy/mm"), "05/01"); Assert.AreEqual(plan.ToString("YYYY年m月"), "2018年1月"); Assert.AreEqual(plan.ToString("公元YyYy年mM月,哈哈..."), "公元2018年01月,哈哈..."); //比较相等 Assert.IsTrue(plan.Equals(Monthly.FromDot(201801))); Assert.IsTrue(plan.Equals(new DateTime(2018, 1, 1))); Assert.IsTrue(plan.Equals((object)Monthly.FromDot(201801))); Assert.IsFalse(plan.Equals(Monthly.FromDot(201901))); } } }

 

Monthly使用介绍

1.Monthly构造

  //创建一个“2018年1月”的账期
   Monthly m1 = 201801;
   Monthly m2 = new Monthly(2018, 1);
   Monthly m3 = Monthly.FromDate(new DateTime(2018, 1, 1));
   Monthly m4 = Monthly.FromDot(201801);
   Monthly m5 = Monthly.FromTickes(2018 * 12 + 1);
   Monthly m6 = Monthly.FromString("2018年01月");
   
   Monthly cur = Monthly.Current;   //当前时间实例
   Monthly min = Monthly.MinValue;  //Monthly最小实例
   Monthly max = Monthly.MaxValue;  //Monthly最大实例

2. Monthly属性

属性 说明
Year 获取当前实例的年
Month 获取当前实例的月
Dot 获取当前实例的年月标记值,如2018年1月记为 : 201801
Tickes 获取当前实例从公元零年一月开始的月份累计值
First 获取当前年份的一月为依据的新实例
Last 获取当前年份的十二月为依据的新实例
Previous 获取当前时间点的上月为依据的新实例
Next 获取当前时间点的下月为依据的新实例
Quarter 获取当前实例所在的季度

3.Monthly方法

  • ToDot();
    说明:获取当前实例的年月标记值,如2018年1月记为 : 201801
  • AddYears(int years)
    说明:以当前实例与years的和值为依据创建一个新实例

  • AddMonths(int months)
    说明:以当前实例与months的和值为依据创建一个新实例
  • Equals(Monthly other)
    说明:判断当前实例的值与给定实例的值是否相等
  • Equals(object obj)
    说明:判断当前实例的值与给定实例的转换值是否相等,obj可以是DateTime类型
  • SpanMonths(Monthly other)
    说明:获取当前实例与给定实例的月份差值
  • SpanMonths(DateTime date)
    说明:获取当前实例与DateTime实例的月份差值
  • CompareTo(Monthly other)
    说明:获取当前实例与给定实例的大小比较的结果标识, -1:小于other实例值 ; 0 等于other实例值 ; 1:大于other实例值
  • List<Monthly> Axis(int from, int to)
    说明:获取一段时间内的Monthly数轴(包含开始与结束月份)
  • List<Monthly> Axis(Monthly from, Monthly to)
    说明:同 List Axis(int from, int to)
  • ToString(string format = "yyyy/mm")
    说明:获取包含"Y、y、M、m"字符格式的自定义Monthly字符串,format 格式如:yyyy/mm ; yy/mm ; yyyy年mm月 ;YYYY-Mm...,不区分大小写

示例:

   Monthly m = 201801;
   m.CompareTo(201701);            
   m.Equals(DateTime.Now);
   m.Equals(201701);
   m.SpanMonths(new DateTime(2017, 1, 1));
   m.SpanMonths(201701);

   m.ToString();
   m.ToString("yy/mm");
   Monthly.FromDot(501).ToString("yy/mm");
   m.ToString("YYYY年m月");
   m.ToString("公元YyYy年mM月,哈哈...");

4.Monthly操作符

Monthly支持+、- 、* 、/ 、> 、>= 、< 、<= 、++ 、-- 、== 、!= 运算符操作。

特别注意:-操作,他有operator -(Monthly m, int months)operator -(Monthly m1, Monthly m2)两个重载版本,且方法功能不同,如果是第二个版本,则必须显式标注被减对象的数据类型,如m-(Monthly)201701

 

参考:
https://referencesource.microsoft.com/#mscorlib/system/datetime.cs,df6b1eba7461813b 微软Datetime数据类型


相关教程