(C# VB)UTC时间戳/时间/秒数/字节数组转换

什么是Unix时间戳(Unix timestamp): Unix时间戳(Unix timestamp),或称Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。Unix时间戳不仅被使用在Unix系统、类Unix系统中,也在许多其他操作系统中被广泛采用
 
参考工具网站:
http://tool.chinaz.com/Tools/unixtime.aspx  Unix时间戳转换
https://tool.oschina.net/hexconvert/  在线进制转换
https://converter.telerik.com/  c#-vb代码在线转换
https://www.cnblogs.com/huanjun/p/9473652.html  c#获取UTC转换时间戳为c#时间
 

VB代码:
Module Module1
    Sub Main()
        Dim dt As DateTime = Now
        Console.WriteLine("当前时间:" & dt.ToString())
        Console.WriteLine("秒数时间戳:" & DateToUtcSeconds(dt))
        Console.WriteLine("字节0:" & GetBytesTimeStampUTC(dt)(0))
        Console.WriteLine("字节1:" & GetBytesTimeStampUTC(dt)(1))
        Console.WriteLine("字节2:" & GetBytesTimeStampUTC(dt)(2))
        Console.WriteLine("字节3:" & GetBytesTimeStampUTC(dt)(3))
        Console.WriteLine("转回整数:" &  BytesToIntStampUTC(GetBytesTimeStampUTC(dt)))
        Console.WriteLine("重新转回时间:" &  SecondsToDateTimeStampUTC(BytesToIntStampUTC(GetBytesTimeStampUTC(dt))))
        Console.ReadKey()
    End Sub
    ''' 
    ''' 给定一个时间,返回其UTC的4个字节
    ''' 
    ''' 
    ''' 
    ''' 
    Public Function GetBytesTimeStampUTC(ByVal thisTime As DateTime) As  Byte()
        '给定一个时间,返回其UTC的4个字节
        Dim dEpock As DateTime = New Date(1970, 1, 1)    '最开始的时间
        Dim startTime As DateTime =  TimeZone.CurrentTimeZone.ToLocalTime(dEpock)
        Dim iSeconds As Integer = CInt((thisTime -  startTime).TotalSeconds)
        Dim bytes As Byte() = New Byte(3) {}     '注意byte(0)为最低位字节,可根据需要倒转
        bytes(0) = iSeconds And &HFF
        bytes(1) = (iSeconds And &HFF00) / &H100
        bytes(2) = (iSeconds And &HFF0000) / &H10000
        bytes(3) = (iSeconds And &HFF000000) / &H1000000 Or (((iSeconds  And &H80000000) <> 0) And &H80)
        Return bytes
    End Function
    ''' 
    ''' 4字节的时间戳转换回整数秒数
    ''' 
    ''' 字节数组,bytes(0)为最低位。如5DCFE2D9,bytes(0)=D9=217
    ''' 
    ''' 
    Public Function BytesToIntStampUTC(ByVal bytesUtc As Byte()) As  Integer
        Dim iSecondsUTC As Integer = bytesUtc(0) + bytesUtc(1) * 256 +  bytesUtc(2) * 65536 + bytesUtc(3) * 16777216
        Return iSecondsUTC
    End Function
    ''' 
    ''' UTC时间戳转换回时间形式
    ''' 
    ''' UTC时间戳,整型 秒数
    ''' 
    ''' 
    Public Function SecondsToDateTimeStampUTC(ByVal iSeconds As Integer)  As DateTime
        Dim StartTime As DateTime =  TimeZone.CurrentTimeZone.ToLocalTime(New DateTime(1970, 1, 1)) '起始时间
        Dim StampTime As DateTime = StartTime.AddSeconds(iSeconds) '起始时间加UTC时间戳秒数
        Return StampTime
    End Function
    ''' 
    ''' 给定时间,获取其UTC时间戳
    ''' 
    ''' 
    ''' 
    ''' 
    Public Function DateToUtcSeconds(ByVal thisTime As DateTime) As  Integer
        Dim dEpock As DateTime = New Date(1970, 1, 1)    '最开始的时间
        Dim startTime As DateTime =  TimeZone.CurrentTimeZone.ToLocalTime(dEpock)
        Dim iSeconds As Integer = CInt((thisTime -  startTime).TotalSeconds)
        Return iSeconds
    End Function
End Module

 


c#代码
       
 
 #region"UTC时间戳转换"
        /// 
        /// 获取UTC时间戳
        /// 
        /// UTC
        public static long   GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0,  0, 0);
            return Convert.ToInt64(ts.TotalSeconds);
            //获取毫秒时间戳:
            //TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0,  0, 0,0);
            //return Convert.ToInt64(ts.TotalMilliseconds ());
        }
        /// 
        /// 转换时间戳为C#时间
        /// 
        /// 时间戳 单位:秒
        /// C#时间
        public static DateTime ConvertTimeStampToDateTime(long  timeStamp)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new  System.DateTime(1970, 1, 1)); // 当地时区
            DateTime dt = startTime.AddSeconds(timeStamp);
            return dt;
        }
        #endregion
        static void Main(string[] args)
        {
            long seconds = GetTimeStamp();
            DateTime dt = ConvertTimeStampToDateTime(seconds);
            Console.WriteLine("当前时间:"+ DateTime.Now.ToString());
            Console.WriteLine("时间戳秒数:"+ seconds);
            Console.WriteLine("转回时间类型:"+ dt.ToString());
            
            Console.ReadKey();
        }

 

 
 
 

你可能感兴趣的:(VB,c#)