double ntime=dateTimeToUnixTimestamp(DateTime.Now);

long g1 = GetUnixTimestamp();
long g2 = ConvertDateTime2Long(DateTime.Now);

public double dateTimeToUnixTimestamp(DateTime datetime)
{
return (datetime - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds/10000;
}

string date = DateTime.Now.AddYears(-20).ToString("yyyy-MM-dd HH:mm:ss");

1995-11-05 20:15:37
/// <summary>
/// 将c# DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time">时间</param>
/// <returns>long</returns>
public long ConvertDateTime2Long(System.DateTime time)
{
DateTime timeStamp = new DateTime(1970, 1, 1); //得到1970年的时间戳
long a = (time.ToUniversalTime().Ticks - timeStamp.Ticks) / 10000000; //注意这里有时区问题,用now就要减掉8个小时
return a;
}

//获取当前时间的时间戳
public long GetUnixTimestamp()
{
DateTime timeStamp = new DateTime(1970, 1, 1); //得到1970年的时间戳
long a = (DateTime.UtcNow.Ticks - timeStamp.Ticks) / 10000; //注意这里有时区问题,用now就要减掉8个小时
return a;
}

            //
long temp = GetUnixTimestamp();
DateTime tamp = GetTime(Convert.ToString(temp)); /// <summary>
/// c# to unix
/// </summary>
/// <returns></returns>
public static long GetUnixTimestamp()
{
DateTime timeStamp = new DateTime(, , ); //得到1970年的时间戳
long a = (DateTime.UtcNow.Ticks - timeStamp.Ticks) / ; //注意这里有时区问题,用now就要减掉8个小时
return a;
}
/// <summary>
/// unix to c#
/// </summary>
/// <param name="timeStamp"></param>
/// <returns></returns>
public static DateTime GetTime(string timeStamp)
{
DateTime dtStart = new DateTime(, , ); //得到1970年的时间戳
long lTime = long.Parse(timeStamp + "");
TimeSpan toNow = new TimeSpan(lTime);
DateTime temp=dtStart.Add(toNow);
System.TimeSpan duration = new System.TimeSpan(, , , );
System.DateTime answer = temp.Add(duration);
return answer;
}

C# DateTime时间格式转换为Unix时间戳格式的更多相关文章

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

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

  2. [工具类]将时间转换为unix时间戳格式

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

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

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

  4. c# datetime与 timeStamp(unix时间戳) 互相转换

    /// <summary> /// Unix时间戳转为C#格式时间 /// </summary> /// <param name="timeStamp" ...

  5. c# DateTime时间格式和JAVA时间戳格式相互转换

    /// java时间戳格式时间戳转为C#格式时间 public static DateTime GetTime(long timeStamp) { DateTime dtStart = TimeZon ...

  6. MySql 格式化时间(包括正常时间格式与unix时间戳的互相转换)

    函数:FROM_UNIXTIME 作用:将MYSQL中以INT(11)存储的时间以"YYYY-MM-DD"格式来显示.语法:FROM_UNIXTIME(unix_timestamp ...

  7. 将windows文本格式转换为UNIX格式

    将windows文本格式转换为UNIX格式 1.使用sed命令来进行转换,如下: sed -e ’s,^M,,g’ textfile 其中^M的输入方法是Ctrl+V, Ctrl+M 对于批量文件的处 ...

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

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

  9. Swagger--解决日期格式显示为Unix时间戳格式 UTC格式

    在swagger UI模型架构上,字段日期显示为“日期”:“2018-10-15T09:10:47.507Z”但我需要将其作为“日期”:“2018-9-26 12:18:48”. tips:以下这两种 ...

随机推荐

  1. 使用logrotate管理nginx日志文件

    本文转载自:http://linux008.blog.51cto.com/2837805/555829 描述:linux日志文件如果不定期清理,会填满整个磁盘.这样会很危险,因此日志管理是系统管理员日 ...

  2. ios NSNotificationCenter 收到通知后的执行线程

    https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/Thread ...

  3. 7.SpringMVC注解优化

    新建controller,每个人都可以建自己的controller,比较方便,不用再在web.xml中进行配置,value的请求地址一定要唯一. 优化: 1.上面示例中spring-annotatio ...

  4. 在Linux下记录所有用户的登录和操作日志

    一般我们可以用history命令来查看用户的操作记录,但是这个命令不能记录是哪个用户登录操作的,也不能记录详细的操作时间,且不完整:所以误操作而造成重要的数据丢失,就很难查到是谁操作的. 在这里我们通 ...

  5. mybatis一对多查询

    18 <!-- 19 方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集 20 封装联表查询的数据(去除重复的数据) 21 select * from class c, teacher ...

  6. 仿美团外卖,饿了吗 两个ListView联动,左边点击切换右边,右边滑动切换左边

    先上效果图: 实现思路: 1.先说右边标题: 首先,右边的数据源集合中的Javabean中含有三个属性name,type,title,而每个条目中会默认含有一个标题. 如果这是第一个条目,就让标题显示 ...

  7. 【leetcode】LRU Cache(hard)★

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  8. IOS-MVC的使用

    1.Model不允许和Controller,View打交道.也就是Model根本不知道谁会用自己,Model中不能有任何对 Controller和View的引用.正所谓:Don't call me, ...

  9. 宠物收养所(bzoj1208)

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

  10. 43个优秀的Swift开源项目

    作为一门集百家之长的新语言,Swift拥有着苹果先天的生态优势,而其在GitHub上各种优秀的开源项目也层出不穷.本文作者@SwiftLanguage从2014年6月苹果发布Swift语言以来,便通过 ...