• TimeHelp 获取时间戳转换类


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Share
    {
        public class TimeHelp
        {
            /// <summary>  
            /// 获取时间戳  10位
            /// </summary>  
            /// <returns></returns>  
            public static string GetTimeStamp()
            {
                TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                return Convert.ToInt64(ts.TotalSeconds).ToString();
            }
    
            /// <summary>
            /// 获取时间戳 13
            /// </summary>
            /// <returns></returns>
            public static string GetTimeStamp(System.DateTime time)
            {
                long ts = ConvertDateTimeToInt(time);
                return ts.ToString();
            }
            /// <summary>  
            /// 将c# DateTime时间格式转换为Unix时间戳格式  
            /// </summary>  
            /// <param name="time">时间</param>  
            /// <returns>long</returns>  
            public static long ConvertDateTimeToInt(System.DateTime time)
            {
                System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
                long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
                return t;
            }
            /// <summary>        
            /// 时间戳转为C#格式时间        
            /// </summary>        
            /// <param name=”timeStamp”></param>        
            /// <returns></returns>        
            public static DateTime ConvertStringToDateTime(string timeStamp)
            {
                DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                long lTime = long.Parse(timeStamp + "0000");
                TimeSpan toNow = new TimeSpan(lTime);
                return dtStart.Add(toNow);
            }
        }
    }
  • 相关阅读:
    测试随笔
    ECNU 3530 和你在一起
    ECNU 1030 母牛生小牛
    ECNU 3081 购房还款
    PPP模式下的融资结构优化
    决策树分类
    关联规则-R语言实现
    中国快递包裹总量的预测-基于SARIMA模型
    LeetCode 2 :Swap Nodes in Pairs
    2018092609-2 选题 Scrum立会报告+燃尽图 04
  • 原文地址:https://www.cnblogs.com/lsgsanxiao/p/9020281.html
Copyright © 2020-2023  润新知