C# 的DateTime 和 Mysql 的10位时间戳 转化

1.将Unix时间戳转换为DateTime类型时间

 

public static System.DateTime ConvertIntDateTime(long d)

{

System.DateTime time = System.DateTime.MinValue;

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));

time = startTime.AddSeconds(d);

return time;

}

 

2.将c# DateTime时间格式转换为Unix时间戳格式

 


        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位      
            long t = (time.Ticks - startTime.Ticks) / 10000000;   //除10000000调整为10位    
            
            return t;
        }

3.Mysql  时间转化

 

1.10位数字转时间
select from_unixtime("1669824447")


2.时间转10位数字
select unix_timestamp("2022-12-1 11:02:56")

 

posted @ 2022-12-16 11:41  DJ的魔鬼邂逅  阅读(432)  评论(0)    收藏  举报