写在前面

由于在数据库中存的时间有时间戳格式的数据,在解析以及保存的时候,就需要考虑到数据格式的兼容性问题。看到数据库中的时间字段基本上都是以时间戳格式存储的,没办法,只能将时间进行转换了,考虑到其他系统可能要对时间字段进行操作,就不得不考虑时间的兼容性了。

时间戳辅助类

什么是时间戳?

Unix时间戳(Unix timestamp),或称Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。Unix时间戳不仅被使用在Unix系统、类Unix系统中(比如Linux系统),也在许多其他操作系统中被广泛采用。
php中获取时间戳的方法是:time();Date();
Linux中获取时间戳的方法是:date +%s
Linux中将时间戳转换为日期: date -d "@<timestamp>"

既然知道了基准时间为1970年01月01日00时00分00秒,那么转换为时间戳就很容易了。

     /// <summary>
///日期转换辅助类
/// </summary>
public static class DateHelper
{ public static DateTime ConvertTime(string datatime)
{
Int64 timeStamp = Convert.ToInt64(datatime);
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(, , ));
TimeSpan toNow = new TimeSpan(timeStamp * );
return dtStart.Add(toNow);
} /// <summary>
/// 将c# DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time">时间</param>
/// <returns>long</returns>
public static long ConvertTime2Long(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , , , , , ));
long t = (time.Ticks - startTime.Ticks) / ; //除10000调整为13位
return t;
} }

简单测试

     class Program
{
static void Main(string[] args)
{
long result = DateHelper.ConvertTime2Long(DateTime.Now);
Console.WriteLine("时间戳:" + result);
DateTime dt = DateHelper.ConvertTime(result.ToString());
Console.WriteLine("时间:"+dt.ToString());
Console.Read();
}
}

[工具类]将时间转换为unix时间戳格式的更多相关文章

  1. C# DateTime时间格式转换为Unix时间戳格式

    double ntime=dateTimeToUnixTimestamp(DateTime.Now); long g1 = GetUnixTimestamp(); long g2 = ConvertD ...

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

    /// <summary> /// 将DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="date ...

  3. 将当天时间转换为unix时间戳

    /** * @return * * @Title: getDate * @Description: TODO(时间戳转换为String类型的日期数据) * @param @param unixDate ...

  4. 字符串时间与Unix时间戳相互转换

    字符串时间与Unix时间戳相互转换 /** * @Author: wangkun * @Date : 2016/1/21 13:43 * @Description : 字符串时间转换为Unix时间戳 ...

  5. c# dateTime格式转换为Unix时间戳工具类

    using System; using System.Collections.Generic; using System.Text; namespace TJCFinanceWriteOff.BizL ...

  6. python正常时间和unix时间戳时间的相互转换源码

    在学习过程,将内容过程比较常用的一些内容做个珍藏,下面的内容段是关于python正常时间和unix时间戳时间的相互转换的内容,应该是对各朋友有些帮助. import time def timestam ...

  7. Jquery实现日期转换为 Unix时间戳及时间戳转换日期

    (function ($) { $.extend({ myTime: { /** * 当前时间戳 * @return <int> unix时间戳(秒) */ CurTime: functi ...

  8. python正常时间和unix时间戳相互转换的方法

    python正常时间和unix时间戳相互转换的方法 本文实例讲述了python正常时间和unix时间戳相互转换的方法.分享给大家供大家参考.具体分析如下: 这段代码可以用来转换常规时间格式为unix时 ...

  9. Linux命令date日期时间和Unix时间戳互转

    A.将日期转换为Unix时间戳将当前时间以Unix时间戳表示: date +%s 输出如下: 1361542433 转换指定日期为Unix时间戳: date -d '2013-2-22 22:14' ...

随机推荐

  1. Windows 系统下json 格式的日志文件发送到elasticsearch

    Windows 系统下json 格式的日志文件发送到elasticsearch配置 Nxlog-->logstash-->ElasticSearch Logstash https://ww ...

  2. WCF宿主端检验队列

    ServiceHost host = new ServiceHost(typeof(Service1)); if (MessageQueue.Exists(@".\private\MyMes ...

  3. hadooop2.6 job pending research

    https://hadoop.apache.org/docs/r2.6.0/hadoop-project-dist/hadoop-common/ClusterSetup.html 我使用的是已经运行在 ...

  4. ZGrapher 画函数曲线图的工具

    可以下载个绿色版,我下载的是 ZGrapher 1.4 绿色版.下面先看下图出来的图: 然后可以在“file"->"Save as Picture ..." -&g ...

  5. QC学习三:Excel数据导入导出QC操作流程

    环境: QC9 WindowsXP Office2007   1. 准备 1.通过Excel导入QC,需要下载Microsoft Excel Add-in: http://update.externa ...

  6. js原生捕鱼达人(三)--完结

    先给分享下我写完的效果,github有点卡,我没有压缩代码,不过效果可以看到 https://jasonwang911.github.io/ 转载请注明'转载于Jason齐齐的博客http://www ...

  7. copy sqlserver中DATE类型的数据转化 CONVERT

    copy http://www.cnblogs.com/benwu/p/3939044.html 主要描述的是SQL Server使用convert取得datetime日期数据的实际操作流程,在实际操 ...

  8. Chrome 开发工具 Javascript 调试技巧

    http://www.w3cplus.com/tools/dev-tips.html 一.Sources 面板介绍: Sources 面板分为左中右 3 部分左:Sources 当前页面加载的资源列表 ...

  9. MVC权限验证之ActionFilterAttribute

    参考:http://www.cnblogs.com/waitingfor/archive/2011/12/27/2303784.html ActionFilterAttribute是Action过滤类 ...

  10. javascript限制上传文件大小

    在FireFox.Chrome浏览器中可以根据document.getElementById(“id_file”).files[0].size 获取上传文件的大小(字节数)使用的 api是FileRe ...