时间转换类【TimeConverter】

 

 

 

 

    /// <summary>
    /// 时间转换类
    /// </summary>
    public class TimeConverter
    {
        /// <summary>
        /// 本地时间转成 - UTC时间、距1970年的毫秒数
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static long DateTimeToMilliseconds(DateTime time)
        {
            try
            {
                if (time.Year < 1970 || time.Year > 2030)
                {
                    time = new DateTime(2000, time.Month, time.Day, time.Hour, time.Minute, time.Second);
                }

                return (long)(time.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalMilliseconds;
            }
            catch (ArgumentOutOfRangeException argu)
            {
                Console.WriteLine(argu.Message);
            }
            catch (ArgumentException argu2)
            {
                Console.WriteLine(argu2.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return (long)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalMilliseconds;
        }

        /// <summary>
        /// 本地时间转成 - UTC时间、距1970年的秒数
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static long DateTimeToSeconds(DateTime time)
        {
            try
            {
                if (time.Year < 1970 || time.Year > 2030)
                {
                    time = new DateTime(2000, time.Month, time.Day, time.Hour, time.Minute, time.Second);
                }

                return (long)(time.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
            }
            catch (ArgumentOutOfRangeException argu)
            {
                Console.WriteLine(argu.Message);
            }
            catch (ArgumentException argu2)
            {
                Console.WriteLine(argu2.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return (long)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
        }


        /// <summary>
        /// UTC距1970年毫秒数 - 转成本地时间
        /// </summary>
        /// <param name="milliseconds"></param>
        /// <returns></returns>
        public static DateTime MillisecondsToDateTime(long milliseconds)
        {
            try
            {
                DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(milliseconds).ToLocalTime();

                if (time.Year < 1970 || time.Year > 2030)
                {
                    time = new DateTime(2000, time.Month, time.Day, time.Hour, time.Minute, time.Second);
                }

                return time;
            }
            catch (ArgumentOutOfRangeException argu)
            {
                Console.WriteLine(argu.Message);
            }
            catch (ArgumentException argu2)
            {
                Console.WriteLine(argu2.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return DateTime.Now;
        }

        /// <summary>
        /// UTC距1970年秒数 - 转成本地时间
        /// </summary>
        /// <param name="seconds"></param>
        /// <returns></returns>
        public static DateTime SecondsToDateTime(long seconds)
        {
            try
            {
                DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(seconds).ToLocalTime();

                if (time.Year < 1970 || time.Year > 2030)
                {
                    time = new DateTime(2000, time.Month, time.Day, time.Hour, time.Minute, time.Second);
                }

                return time;
            }
            catch (ArgumentOutOfRangeException argu)
            {
                Console.WriteLine(argu.Message);
            }
            catch (ArgumentException argu2)
            {
                Console.WriteLine(argu2.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return DateTime.Now;
        }

    }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值