将时间格式转化为一个int类型
// ::26时间转完后为:1389675686数字

为什么使用时间戳?

关于Unix时间戳,大概是这个意思,从1970年0时0分0秒开始到现在的秒数.使用它来获得的是一个INT值,储存在数据库里只要使用INT格式就可以了,方便数据库进行排序,搜索,而且比datetime格式更节省数据库空间。

正式使用的代码,获得毫秒数:

/// <summary>
/// 生成时间戳
/// </summary>
/// <returns>当前时间减去 1970-01-01 00.00.00 得到的毫秒数</returns>
public string GetTimeStamp()
{
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , , , , , ));
DateTime nowTime = DateTime.Now;
long unixTime = (long)System.Math.Round((nowTime - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero);
return unixTime.ToString();
}

以下为 他人 博客代码:

以下是测试过的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace test.Controllers
{
public class TimeStampController : Controller
{
//
// GET: /TimeStamp/ public ActionResult Index()
{
ViewBag.TimeStamp = ConvertDateTimeInt(DateTime.Now);
return View("TimeStamp");
} public ActionResult GetTimeView(string timeStamp)
{
ViewBag.TimeStamp = GetTime(timeStamp);
return View("TimeStamp");
} /// <summary>
/// 时间戳转为C#格式时间
/// </summary>
/// <param name="timeStamp">Unix时间戳格式</param>
/// <returns>C#格式时间</returns>
public static DateTime GetTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(, , ));
long lTime = long.Parse(timeStamp + "");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
} /// <summary>
/// DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time"> DateTime时间格式</param>
/// <returns>Unix时间戳格式</returns>
public static int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , ));
return (int)(time - startTime).TotalSeconds;
} }
}

double格式存储

static double ToTimestamp(DateTime value)
{
TimeSpan span = (value - new DateTime(, , , , , , ).ToLocalTime());
return (double)span.TotalSeconds;
} static DateTime ConvertTimestamp(double timestamp)
{
DateTime converted = new DateTime(, , , , , , );
DateTime newDateTime = converted.AddSeconds(timestamp);
return newDateTime.ToLocalTime();
}

参考:18妹的小窝     c# datetime与 timeStamp 互相转换

c# datetime与 timeStamp时间戳 互相转换的更多相关文章

  1. Python datetime与timestamp之间的转换

    # !!!! Python 2 datetime.datetime 对象没有timestamp方法! 在用Python处理datetime和timestamp的转换时发现在时区方面,Python的处理 ...

  2. C# DateTime类型和时间戳 互相转换

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

  3. C#DateTime与Unix时间戳的转换

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

  4. c#DateTime与unix时间戳互相转换

    public class UnixTimeUtil { /// <summary> /// 将dateTime格式转换为Unix时间戳 /// </summary> /// & ...

  5. datetime,Timestamp和datetime64之间转换

    引入工具包 import datetime import numpy as np import pandas as pd 总览 from IPython.display import Image fr ...

  6. Mysql - 如何决定用 datetime、timestamp、int 哪种类型存储时间戳?

    背景 数据表都很可能会有一两个字段需要保存日期时间数据,那应该用什么 Mysql 类型来保存呢? 前面讲过 datetime.timestamp.int 的方式来保存日期时间 如何存储 10位.13位 ...

  7. DateTime与timeStamp的转换

    DateTime转换为timeStamp: DateTime dt = DateTime.Now;            DateTime startTime = TimeZone.CurrentTi ...

  8. .net 时间戳互相转换(精确到毫秒)

    这里记录一个时间戳的互相转换方法,网上都找了,基本都没有精确到毫秒,我的这个基本可以满足精确到毫秒的级别,代码如下: /// <summary> /// Unix时间戳转换为DateTim ...

  9. Sublime Text3时间戳查看转换插件的开发

    平常配置表中,经常需要用到时间配置,比如活动开始结束.从可读性上,我们喜欢2017-04-27 17:00:00,从程序角度,我们喜欢用1493283600.前者是包含时区概念的,而后者市区无关,所以 ...

随机推荐

  1. 原创Oracle数据泵导出/导入(expdp/impdp)

    //创建目录 create Or Replace directory dpdata1 as 'd:\test\dump'; //赋予读写权限 grant read,write on directory ...

  2. fread与read的差别(文件io补充)

    这里有一个我们常常提出的问题就是fread,read的差别.(当然这两个分别代表了操作文件系统的两套不同的函数,包含open,read, write, seek 等). 一.他们的差别就是一个(rea ...

  3. 如何在Win8中设置虚拟热点共享上网(转)

    摘自:http://www.enet.com.cn/article/2013/0408/A20130408273749.shtml 在Windows 7中,我们可以通过网络与共享中心的“设置新的连接和 ...

  4. PHP-中文在计算机中的存储

    经常我们打开外国网站的时候出现乱码,又或者打开很多非英语的外国网站的时候,显示的都是口口口口口的字符, WordPress程序是用的UTF-8,很多cms用的是GB2312. ● 为什么有这么多编码? ...

  5. webview使用遇到 It is possible that this object was over-released, or is in the process of deallocation错误的解决办法

    使用wekwebview时,push后,再pop返回,报错了: Cannot form weak reference to instance (xxxx) of class xxxx. It is p ...

  6. DBA_实践指南系列7_Oracle Erp R12监控OAM(案例)

    2013-12-07 Created By BaoXinjian

  7. Go 普通LOG输出

    因为Go 语言中没有自带的宏, 来表示行号和文件, 需要从方法中去获取,麻烦.所以封装了一个函数,用于输出平时程序的打印日志 import ( "fmt" "log&qu ...

  8. W5500初始化过程

  9. Mac - 使用php环境

    按下shift + Command + G  ,输入以下php(www)路径 /Library/WebServer/Documents 打开终端,输入以下命令打开环境 sudo apachectl s ...

  10. 实例讲解Nginx下的rewrite规则(转)

    一.正则表达式匹配,其中:* ~ 为区分大小写匹配* ~* 为不区分大小写匹配* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配二.文件及目录匹配,其中:* -f和!-f用来判断是否存在文件* ...