asp.net 时间戳与datetime 转换

/// 
        /// 时间转时间戳格式
        /// 
        /// 数字
        /// 1:秒级别,非1:毫秒级别
        /// 
        public static DateTime ConvertLongToDateTime(long time, int secondsOrMilliseconds = 1)
        {
            TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            DateTime datetime = DateTime.MinValue;
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0,0, DateTimeKind.Utc));
            if (secondsOrMilliseconds == 1)
                datetime = startTime.AddSeconds(time);
            else
                datetime = startTime.AddMilliseconds(time);
            return datetime;
        }

        /// 
        /// 时间戳转时间格式
        /// 
        /// 日期
        /// 1:秒级别,非1:毫秒级别
        /// 
        public static long ConvertDateTimeToLong(DateTime datetime, int secondsOrMilliseconds = 1)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0,0, DateTimeKind.Utc));
            TimeSpan ts = (datetime - startTime);
            if (secondsOrMilliseconds == 1)
                return (long)ts.TotalSeconds;
            else
                return (long)ts.TotalMilliseconds;
        }

你可能感兴趣的:(.net)