将时间格式转化为一个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. 监测CentOS下TCP断线

    TCP正常的断开,通信双方(服务端和客户端)都是能知道的.但是非正常的断开,比如直接拔掉了网线,就只能靠如下两种方法,实现短时间内的检测. 一.心跳包机制 心跳包机制,是网游设计中的常用机制.从用户层 ...

  2. java8 map flatmap

    map: 对于Stream中包含的元素使用给定的转换函数进行转换操作,新生成的Stream只包含转换生成的元素.这个方法有三个对于原始类型的变种方法,分别是:mapToInt,mapToLong和ma ...

  3. Tomcat 配置加密的服务器连接器

    先查询API,找到Configuration里面的Connector的HTTP中的SSL(加密连接器) SSL abbr. Security Socket Layer 加密套接字协议层   利用已生成 ...

  4. 赵雅智_android多线程下载带进度条

    progressBar说明 在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度. 一个进度条也可不确定其进度.在不确定模式下, ...

  5. 原创:微信小程序+WEB使用JS实现注册【60s】倒计时功能

    1.效果图: 2.页面仅仅利用了JS的相关功能,包含:wxml.js.wxss 2.1wxml页面代码: <text>绑定手机</text> <form bindsubm ...

  6. 关于多线程中使用ArrayList的问题

    多线程全局变量:1.定义只读的全局变量时,必须加final修饰,即使是private的,防止被反射修改.2.对于需要多次读写的全局变量,一定要用ThreadLocal封装,避免多线程并发时变量被多次赋 ...

  7. SecureCRT 快捷键总结 设置快捷键

    http://oldboy.blog.51cto.com/2561410/907098 自己整理的一些 查看 Alt + Enter     全屏   菜单 Alt + f + n     克隆会话窗 ...

  8. .NET 跨平台RPC框架DotNettyRPC Web后台快速开发框架(.NET Core) EasyWcf------无需配置,无需引用,动态绑定,轻松使用 C# .NET 0配置使用Wcf(半成品) C# .NET Socket 简单实用框架 C# .NET 0命令行安装Windows服务程序

    .NET 跨平台RPC框架DotNettyRPC   DotNettyRPC 1.简介 DotNettyRPC是一个基于DotNetty的跨平台RPC框架,支持.NET45以及.NET Standar ...

  9. Linux时间子系统(五) POSIX Clock

    一.前言 clock是timer的基础,任何一个timer都需要运作在一个指定的clock上来.内核中维护了若干的clock,本文第二章描述了clock的基本概念和一些静态定义的posix clock ...

  10. WinForm开发,窗体显示和窗体传值相关知识总结

    主窗体中代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...