基本信息
源码名称:PHP中的 dateline 转换成 c#中的 datetime 方法 UNIX时间转换
源码大小:1.10KB
文件格式:.txt
开发语言:C#
更新时间:2013-11-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

在众多的PHP MySQL的应用之中,存储在MySQL中的时间都是一串数字,后经查这个格式的日期叫做:Unix Timestamp;Unix的timestamp是一组数字,表示从1970年1月1日以来的秒数。今天在进行C#应用开发时需要对MySQL中的数据进行操作,写出以下方法供大家参考。

主要应用到的类库有:
System.TimeZone
应用的方法:
返回对应于指定协调通用时间 (UTC) 的本地时间。
public virtual DateTime ToLocalTime(
   DateTime time
);


1、将系统时间转换成UNIX时间戳

            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
            DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
            TimeSpan toNow = dtNow.Subtract(dtStart);
            string timeStamp = toNow.Ticks.ToString();
            timeStamp = timeStamp.Substring(0,timeStamp.Length - 7);

    
2、将UNIX时间戳转换成系统时
            string timeStamp = "1176686120";
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
            long lTime = long.Parse(timeStamp "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime dtResult = dtStart.Add(toNow);