快捷搜索:
来自 计算机编程 2019-08-17 18:32 的文章
当前位置: 67677新澳门手机版 > 计算机编程 > 正文

计算时间差,Monthly数据类型

测试

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Pilipa.Utility.Test
{
    [TestClass]
    public class MonthlyTest
    {
        [TestMethod]
        public void TestProps()
        {
            var tar = DateTime.Now;
            Monthly plan = Monthly.FromDate(tar);

            Assert.AreEqual(Monthly.Current, new Monthly(tar.Year, tar.Month));

            Assert.AreEqual(plan.Year, tar.Year);
            Assert.AreEqual(plan.Month, tar.Month);
            Assert.AreEqual(plan.Dot, tar.Year * 100   tar.Month);
            Assert.AreEqual(plan.Tickes, tar.Year * 12   tar.Month);

            Assert.AreEqual(plan.First.ToDot(), tar.Year * 100   1);
            Assert.AreEqual(plan.Last.ToDot(), tar.Year * 100   12);


            Assert.AreEqual(plan.First.Previous.ToDot(), tar.AddYears(-1).Year * 100   12);
            Assert.AreEqual(plan.Last.Next.ToDot(), tar.AddYears(1).Year * 100   1);

            Assert.AreEqual(plan.Quarter, GetQuarter(tar.Month));

            Assert.AreEqual(Monthly.MinValue, new Monthly(0, 1));
            Assert.AreEqual(Monthly.MaxValue, new Monthly(9999, 12));
        }

        private int GetQuarter(int q)
        {
            if (new System.Collections.Generic.List<int>() { 1, 2, 3 }.Contains(q)) return 1;
            if (new System.Collections.Generic.List<int>() { 4, 5, 6 }.Contains(q)) return 2;
            if (new System.Collections.Generic.List<int>() { 7, 8, 9 }.Contains(q)) return 3;
            if (new System.Collections.Generic.List<int>() { 10, 11, 12 }.Contains(q)) return 4;
            return 0;
        }


        [TestMethod]
        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)));
        }
    }
}

负数

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最大实例

取名空间:System 程序集:mscorlib(在 mscorlib.dll 中)

Monthly源码

using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace System
{
    /// <summary>
    /// 与月份相关的对象,如账单、账期、月刊、月报等
    /// </summary>
    [Serializable]
    [StructLayout(LayoutKind.Auto)]
    public struct Monthly : IComparable<Monthly>, IEquatable<Monthly>
    {
        private int _year;
        private int _month;

        #region Property
        /// <summary>
        ///  获取当前实例的年
        /// </summary>
        public int Year => _year;

        /// <summary>
        ///  获取当前实例的月
        /// </summary>
        public int Month => _month;

        /// <summary>
        /// 获取当前实例的年月标记值,如2018年1月记为 : 201801
        /// </summary>
        public int Dot => this._year * 100   this._month;

        /// <summary>
        /// 获取当前实例从公元零年一月开始的月份累计值
        /// </summary>
        public int Tickes => this._year * 12   this._month;

        /// <summary>
        ///  获取当前实例所在的季度
        /// </summary>
        public int Quarter => (this._month - 1) / 3   1;
        #endregion

        #region Ctor
        /// <summary>
        ///  以指定的年和月初始化Monthly实例。
        /// </summary>
        /// <param name="year"> 年(0 到 9999)</param>
        /// <param name="month"> 月(1 到 12)</param>
        public Monthly(int year, int month)
        {
            CheckYear(year);
            CheckMonth(month);
            this._year = year;
            this._month = month;
        }

        /// <summary>
        /// 获取以当前时间点为依据的新实例
        /// </summary>
        public static Monthly Current => new Monthly() { _year = DateTime.Now.Year, _month = DateTime.Now.Month };

        /// <summary>
        /// 获取当前时间点的上月为依据的新实例
        /// </summary>
        public Monthly Previous => Monthly.fromTickes(this.Tickes - 1);

        /// <summary>
        /// 获取当前时间点的下月为依据的新实例
        /// </summary>
        public Monthly Next => Monthly.fromTickes(this.Tickes   1);

        /// <summary>
        /// 获取当前年份的一月为依据的新实例  
        /// </summary>
        public Monthly First => new Monthly() { _year = this._year, _month = 1 };

        /// <summary>
        /// 获取当前年份的十二月为依据的新实例
        /// </summary>
        public Monthly Last => new Monthly() { _year = this._year, _month = 12 };

        /// <summary>
        /// 获取Monthly的最小值实例
        /// </summary>
        public static Monthly MinValue => new Monthly() { _year = 0, _month = 1 };

        /// <summary>
        /// 获取Monthly的最大值实例
        /// </summary>
        public static Monthly MaxValue => new Monthly() { _year = 9999, _month = 12 };
        #endregion

        #region Method
        private static int yearOfDot(int dot) => dot / 100;

        private static int monthOfDot(int dot) => dot % 100;

        /// <summary>
        /// 获取当前实例的年月标记值,如2018年1月记为 : 201801
        /// </summary>
        /// <returns></returns>
        public int ToDot() => this.Dot;

        /// <summary>
        /// 以当前实例与years的和值为依据创建一个新实例
        /// </summary>
        public Monthly AddYears(int years) => Monthly.FromTickes(this.Tickes   years * 12);

        /// <summary>
        /// 以当前实例与months的和值为依据创建一个新实例
        /// </summary>
        public Monthly AddMonths(int months) => Monthly.FromTickes(this.Tickes   months);

        /// <summary>
        /// 判断当前实例的值与给定实例的值是否相等
        /// </summary>
        public bool Equals(Monthly other) => this.Tickes == other.Tickes;

        /// <summary>
        /// 获取当前实例与给定实例的月份差值
        /// </summary>
        public int SpanMonths(Monthly other) => this - other;

        /// <summary>
        /// 获取当前实例与DateTime实例的月份差值
        /// </summary>
        public int SpanMonths(DateTime date) => this.Tickes - date.Year * 12 - date.Month;

        /// <summary>
        /// 获取当前实例与给定实例的大小比较的结果标识
        /// </summary>
        /// <param name="other"></param>
        /// <returns>-1:小于other实例值 ; 0 等于other实例值 ; 1:大于other实例值</returns>
        public int CompareTo(Monthly other)
        {
            if (this.Tickes < other.Tickes) return -1;
            if (this.Tickes > other.Tickes) return 1;
            else return 0;
        }

        /// <summary>
        /// 以年月标记值创建一个Monthly新实例
        /// </summary>
        /// <param name="dot">格式:201801</param>
        /// <returns></returns>
        public static Monthly FromDot(int dot)
        {
            var year = yearOfDot(dot);
            var month = monthOfDot(dot);
            if (year < 0 || year > 9999 || month < 1 || month > 12)
                throw new ArgumentOutOfRangeException("dot", dot, "Please enter correct dot format such as '201801'.");

            return new Monthly
            {
                _year = yearOfDot(dot),
                _month = monthOfDot(dot)
            };
        }

        private static Monthly fromTickes(int tickes)
        {
            return new Monthly
            {
                _year = (tickes - 1) / 12,
                _month = tickes % 12 == 0 ? 12 : tickes % 12
            };
        }

        /// <summary>
        /// 以年月累计值创建一个Monthly新实例
        /// </summary>
        /// <param name="tickes">以公元零年一月为起点的月份计数值(1-120000)</param>
        public static Monthly FromTickes(int tickes)
        {
            if (tickes < 1 || tickes > 120000)
                throw new ArgumentOutOfRangeException("tickes", tickes, "The tickes must beteen 1 and 120000 .");
            return fromTickes(tickes);
        }

        /// <summary>
        /// 以DateTime实例创建一个Monthly新实例
        /// </summary>
        public static Monthly FromDate(DateTime time) => new Monthly() { _year = time.Year, _month = time.Month };

        /// <summary>
        /// 以诸如"2018/01"格式的字符串创建一个Monthly新实例
        /// </summary>
        /// <param name="s">"2018/01"格式的字符串</param>
        /// <param name="spliter">分隔符</param>
        public static Monthly FromString(string s)
        {
            if (string.IsNullOrEmpty(s))
                throw new Exception("The parameter cannot be null or empty.");

            var nums = Regex.Matches(s, "[0-9] ");
            if (nums.Count == 0)
                throw new Exception("Please give the correct parameters, such as '2018/01' .");

            if (nums.Count == 1)
                return new Monthly(0, Convert.ToInt32(nums[0].ToString().TrimStart('0')));
            else
                return new Monthly(Convert.ToInt32(nums[0].ToString().TrimStart('0')), Convert.ToInt32(nums[1].ToString().TrimStart('0')));
        }

        /// <summary>
        /// 获取一段时间内的Monthly数轴(包含开始与结束月份)
        /// </summary>
        /// <param name="from">开始月份</param>
        /// <param name="to">结束月份</param>
        /// <returns></returns>
        public static List<Monthly> Axis(Monthly from, Monthly to)
        {
            var result = new List<Monthly>();
            var span = from - to;
            var len = (span ^ (span >> 31)) - (span >> 31)   1;
            for (int i = 0; i < len; i  )
            {
                if (span > 0) result.Add(from - i);
                else result.Add(from   i);
            }
            return result;
        }

        /// <summary>
        /// 获取给定时间段内的Monthly集合(包含开始与结束月份)
        /// </summary>
        /// <param name="from">开始月份</param>
        /// <param name="to">结束月份</param>
        /// <returns></returns>
        public static List<Monthly> Axis(int from, int to)
        {
            return Axis(Monthly.FromDot(from), Monthly.FromDot(to));
        }

        /// <summary>
        /// 检查year的合法性
        /// </summary>
        private static void CheckYear(int year)
        {
            if (year < 0 || year > 9999)
                throw new ArgumentOutOfRangeException("year", year, "The year must beteen 0 and 9999 .");
        }

        /// <summary>
        /// 检查month的合法性
        /// </summary>
        private static void CheckMonth(int month)
        {
            if (month < 1 || month > 12)
                throw new ArgumentOutOfRangeException("month", month, "The month must beteen 1 and 12 .");
        }
        #endregion

        #region Operator
        /// <summary>
        /// 以给定实例与months的和值创建一个新实例
        /// </summary>
        /// <param name="months">月分数</param>
        public static Monthly operator  (Monthly m, int months) => FromTickes(m.Tickes   months);

        /// <summary>
        /// 以给定实例与months的差值创建一个新实例
        /// </summary>
        /// <param name="months">月分数</param>
        public static Monthly operator -(Monthly m, int months) => FromTickes(m.Tickes - months);

        /// <summary>
        /// 获取当前实例与给定实例的月份差值
        /// </summary>
        public static int operator -(Monthly m1, Monthly m2) => m1.Tickes - m2.Tickes;

        /// <summary>
        ///获取当前实例与DateTime实例的月份差值
        /// </summary>
        public static int operator -(Monthly m, DateTime d) => m.SpanMonths(d);

        public static Monthly operator   (Monthly m) => m   1;

        public static Monthly operator --(Monthly m) => m - 1;

        /// <summary>
        ///判断m1是否等于m2
        /// </summary>
        public static bool operator ==(Monthly m1, Monthly m2) => m1.Tickes == m2.Tickes;

        /// <summary>
        /// 判断m1是否不等于m2
        /// </summary>
        public static bool operator !=(Monthly m1, Monthly m2) => m1.Tickes != m2.Tickes;

        /// <summary>
        /// 判断m1是否小于m2
        /// </summary>
        public static bool operator <(Monthly m1, Monthly m2) => m1.Tickes < m2.Tickes;

        /// <summary>
        /// 判断m1是否大于m2
        /// </summary>
        public static bool operator >(Monthly m1, Monthly m2)
        {
            return m1.Tickes > m2.Tickes; ;
        }

        /// <summary>
        /// 判断m1是否小于等于m2
        /// </summary>
        public static bool operator <=(Monthly m1, Monthly m2)
        {
            return m1.Tickes <= m2.Tickes; ;
        }

        /// <summary>
        /// 判断m1是否大于等于m2
        /// </summary>
        public static bool operator >=(Monthly m1, Monthly m2)
        {
            return m1.Tickes >= m2.Tickes; ;
        }

        /// <summary>
        /// 以年月标识的Monthly实例
        /// </summary>
        /// <param name="dot">格式:201801</param>
        public static implicit operator Monthly(int dot)
        {
            return Monthly.FromDot(dot);
        }
        #endregion

        #region Override
        /// <summary>
        /// 获取包含"Y、y、M、m"字符格式的自定义Monthly字符串
        /// </summary>
        /// <param name="format">
        /// 如:yyyy/mm ; yy/mm ; yyyy年mm月 ;YYYY-Mm...
        /// 不区分大小写
        /// </param>
        /// <returns></returns>
        public string ToString(string format = "yyyy/mm")
        {
            return Format(this, format);
        }

        /// <summary>
        /// 判断当前实例的值与给定实例的转换值是否相等
        /// </summary>
        public override bool Equals(object obj)
        {
            if (obj is null) throw new ArgumentNullException("obj", "The parameter cannot be null.");
            if (obj is Monthly) return this == (Monthly)obj;
            if (obj is DateTime) return this == Monthly.FromDate((DateTime)obj);
            throw new ArgumentException("The parameter must be System.DateTime type or System.Monthly type .", "obj");
        }

        public override int GetHashCode()
        {
            Int64 ticks = Tickes;
            return unchecked((int)ticks) ^ (int)(ticks >> 32);
        }

        private static string Format(Monthly m, string format)
        {
            string _y = m.Year.ToString();
            string _m = m.Month.ToString();
            format = format.ToLower();
            if (!(format.Contains("yyyy") || format.Contains("yyyy")) && !(format.Contains("mm") || format.Contains("m")))
                throw new ArgumentException("The format expression error. ", nameof(format));
            if (format.Contains("yyyy"))
                format = format.Replace("yyyy", m.Year < 10 ? $"0{_y}" : _y);
            else if (format.Contains("yy"))
                format = format.Replace("yy", m.Year < 10 ? $"0{_y}" : _y.PadLeft(4, '0').Substring(2));
            if (format.Contains("mm"))
                format = format.Replace("mm", m.Month < 10 ? $"0{_m}" : _m);
            else if (format.Contains("m"))
                format = format.Replace("m", _m.TrimStart('0'));
            return format;
        }
        #endregion
    }
}

Duration:获取TimeSpan的相对值。

Monthly使用介绍

TotalSeconds 正是把 Ticks 换算成秒数,即:86399玖仟0000 / (一千0000) = 863999。

Monthly简介

Monthly是三个跟Datetime类似的,与月份相关的数据类型,适用于发挥年月数据,如账单、账期、月刊等音讯。

View Code

Monthly由来

近些日子在做关于智能财政与税收的花色,大量用到了账期相关的多寡操作。项目已有五年历史了,对于账期数据,前辈们用的是DateTime数据类型,即每个月的最终一天就是账期。而用DateTime来发表账期数据,确实让自己人很纳闷:

  1. 概念不统一:
    DateTime是时刻等级次序,而账期只跟时间有关,DateTime用在这里确确实实有一些杀鸡用了宰牛刀,并且给人的明白和挂钩促成了附加的误会。
  2. 格式不联合:
    为了在数码传输和仓库储存中完毕数据的统一性,供给大批量的字符串与日期的改造、日期格式的转变。
  3. 荒疏品质:
    DateTime的正确度是能够到皮秒级的,而小编辈的账期数据只需求规范到月,如:二〇一八年一月账期。 所以DateTime是很影响运算质量和仓库储存空间的。
  4. 操作十二分:
    出于账期是取月末日期,所以对每趟收到了账期参数都要取月末值,以确定保障数据的准头。而在实际上开销中,任何一个大意都会引发表达偏差。

DateTime和TimeSpan是Visual Basic .Net中用于管理时间日期类型数据的一个关键的组织,那二者的界别在于,DatTime代表贰个定位的小时,而TimeSpan表示的是一个小时间隔, 即一段时间。在上边介绍的次序示例中,TimeSpan就用来当前时刻和加以时间之差。

3.Monthly方法

  • ToDot();
    说明:得到当前实例的小时标志值,如二〇一八年七月记为 : 201801
    67677新澳门手机版 ,​
  • 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

参考:
微软Datetime数据类型

2. Date数值中的日期数据可有可无,假如有必须符合格式"m/d/yyyy"。

2. Monthly属性

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

代码实例

TotalSeconds:再次来到TimeSpan值表示的秒数。

TimeSpan 结构  表示二个光阴距离。

 

 

C#中运用TimeSpan总结五个时间的差值

用时间 2 减时间 1,获得三个 TimeSpan 实例。

Seconds:重临用秒计算的TimeSpan值。

ASP.NET 中,多个小时相减,获得一个TimeSpan 实例,提姆eSpan 有部分性子:Days、TotalDays、Hours、TotalHours、Minutes、TotalMinutes、Seconds、TotalSeconds、Ticks,注意未有 TotalTicks。

那么,Days 就是 9,Hours 就是 23,Minutes 就是 59,Seconds 就是 59。

else if (span.TotalDays > 1)
{
return string.Format("{0}天前",
(int)Math.Floor(span.TotalDays));
}
else if (span.TotalHours > 1)
{
return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
}
else if (span.TotalMinutes > 1)
{
return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
}
else if (span.TotalSeconds >= 1)
{
return string.Format("{0}秒前",
(int)Math.Floor(span.TotalSeconds));
}

return "1个月前";
}
else if (span.TotalDays > 14)
{
return
"2周前";
}
else if (span.TotalDays > 7)
{
return "1周前";
}

TotalHours:重临TimeSpan值表示的小时数。

Add:与另二个TimeSpan值相加。

比方表达

二.DateTime和TimeSpan中的常用成员及其表明

Add 将点名的TimeSpan的值加到此实例的值上。
AddDays 将点名的天数加到此实例的值上。
AddHours 将钦命的时辰数加到此实例的值上。
AddMilliseconds 将点名的阿秒数加到此实例的值上。
AddMinutes 将钦赐的分钟数加到此实例的值上。
AddMonths 将点名的月份数加到此实例的值上。
AddSeconds 将点名的秒数加到此实例的值上。
AddYears 将钦赐的年度数加到此实例的值上。
DaysInMonth 再次来到钦点年份中钦赐月份的运气。
IsLeapYear 重返钦点的年度是或不是为闰年的提示。
Parse 将日期和时间的内定字符串表示调换到其一样的DateTime实例。
Subtract 从此实例中减去钦命的年华或持续时间。
ToLongDateString 将此实例的值调换为其同一的长日子字符串表示格局。
ToLongTimeString 将此实例的值转变为其同样的长日子字符串表示格局。
ToShortTimeString 将此实例的值转变为其一样的长时间字符串表示情势。
ToShortDateString 将此实例的值转变为其同样的短日期字符串表示方式。

TotalMinutes:重临TimeSpan值表示的分钟数。

3. Date数值中的时间数额可有可无,倘使有必须和日期数据经过空格分开,并且时分秒之间以":"分开。

里头的七个重载方法之一的构造 提姆eSpan( int days, int hours, int minutes, int seconds )

else {
return "1秒前";

比如说1天12时辰结果便是1.5

•时间 1 是 2010-1-2 8:43:35;

TimeSpan ts = new TimeSpan(12, 12, 11, 12, 21);
            int days = new TimeSpan(12, 12, 11, 12, 21).Days;
            TimeSpan duration = new TimeSpan(-12, 12, 11, 12, 21).Duration();//24小时的反值
            double totalDays = new TimeSpan(12, 12, 11, 12, 21).TotalDays;
            double totalHours = new TimeSpan(2, 12, 11, 12, 21).TotalHours;
            int hours = new TimeSpan(12, 12, 11, 12, 21).Hours;
            int minutes = new TimeSpan(12, 12, 11, 12, 21).Minutes;
            TimeSpan ts2 = new TimeSpan(1, 3, 01, 12, 21);
           // TimeSpan ts2 = new TimeSpan(5, 3, 1, 12, 21);//this is also right
            TimeSpan ts3 = new TimeSpan(5, 3, 01, 12, 21).Add(ts);
            TimeSpan ts4 = new TimeSpan(4, 3, 01, 12, 21).Subtract(ts);
            TimeSpan ts5 = new TimeSpan(4, 3, 01, 12, 21).Subtract(ts).Duration();
            TimeSpan Negate = new TimeSpan(5, 3, 01, 12, 21).Negate();
            DateTime dtnow = DateTime.Now;
            Console.WriteLine(ts);
            Console.WriteLine(days);
            Console.WriteLine(duration);
            Console.WriteLine(totalDays);
            Console.WriteLine(totalHours);
            Console.WriteLine(hours);
            Console.WriteLine(minutes);
            Console.WriteLine(ts2);
            Console.WriteLine(ts ts2);
            Console.WriteLine(ts3);
            Console.WriteLine("Subtract:" ts4);
            Console.WriteLine("Duration:"   ts5);
            Console.WriteLine(ts - ts2);
            Console.WriteLine(Negate);
            DateTime span = DateTime.Now.Add(ts2);
            TimeSpan spdate = span - DateTime.Now;
            long dateTicks = DateTime.Now.Add(ts2).Ticks;
            Console.WriteLine(span);
            Console.WriteLine(spdate);
            Console.WriteLine(dateTicks);

能够反加五个日子之间任何三个时日单位。
private string DateDiff(DateTime DateTime1, DateTime DateTime2)
{string dateDiff = null;
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new
TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
dateDiff = ts.Days.ToString() "天" ts.Hours.ToString() "小时" ts.Minutes.ToString() "分钟" ts.Seconds.ToString() "秒";
return dateDiff;
}

Negate:重返当前实例的相反数。

本文由67677新澳门手机版发布于计算机编程,转载请注明出处:计算时间差,Monthly数据类型

关键词: