c#时间戳转换

2012-09-17 444 0

今天遇到个比较搞的事情,我们接口层的时间格式采用时间戳的格式来进行定义的。在各个语言上面应该是比较通用的吧!

我们讨论的焦点是c#不好处理,要把格式改成yyyy-mm-dd hh:mm:ss的格式,就这样产生了分歧。

我不做C#开发,也承认C#作为MS的产品,可能C#对于时间戳会没做很好的处理,可总有处理的办法吧!呀!这个问题纠结的啊!

为了测试有办法处理,给电脑装了VS2010,测试之后,才发现,这么容易处理! 发代码给大家瞧瞧,没学过代码的也能明白,啥叫个简单。

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("时间戳转换为本地时间");            Console.WriteLine("1347872069"+"转换为本地时间:");            long l = 1347872069; // 本地时间为:2012/9/17 8:54:29            Console.WriteLine(FROM_UNIXTIME(l));            Console.WriteLine("=================");            Console.WriteLine("本地时间转换为时间戳");            DateTime dt = DateTime.Now;//取系统当前时间            Console.WriteLine("系统当前时间:" + dt.ToLocalTime());            Console.WriteLine(UNIX_TIMESTAMP(dt));            Console.Read();        }        //时间戳转换为本地时间        public static DateTime FROM_UNIXTIME(long timeStamp)        {            return DateTime.Parse("1970-01-01 00:00:00").AddSeconds(timeStamp);        }        //本地时间转换为时间戳        public static long UNIX_TIMESTAMP(DateTime dateTime)        {            return (dateTime.Ticks - DateTime.Parse("1970-01-01 00:00:00").Ticks) / 10000000;        }    }}



提供一个时间戳转换工具的网址,不相信,可以试试。

http://shiningray.cn/toys/unix-timestamp

相关文章

15年来的手艺之路:手艺人赵鹏的自述
纪念 Google 25 周年:从搜索引擎到科技巨头的演变之路
1小时编写一个支持七牛上传的 markdown 客户端3(打包发布篇)
1小时编写一个支持七牛上传的 markdown 客户端2(代码优化篇)
1小时编写一个支持七牛上传的 markdown 客户端1(技术实现篇)
从 wordpress 转移到 hexo

Leave a Reply