c# datetime与 timeStamp时间戳 互相转换
将时间格式转化为一个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时间戳 互相转换的更多相关文章
- Python datetime与timestamp之间的转换
# !!!! Python 2 datetime.datetime 对象没有timestamp方法! 在用Python处理datetime和timestamp的转换时发现在时区方面,Python的处理 ...
- C# DateTime类型和时间戳 互相转换
/// <summary> /// 时间戳转为C#格式时间 /// </summary> /// <param name=”timeStamp”></para ...
- C#DateTime与Unix时间戳的转换
/// <summary> /// Unix时间戳转为C#格式时间 /// </summary> /// <param name="timeStamp" ...
- c#DateTime与unix时间戳互相转换
public class UnixTimeUtil { /// <summary> /// 将dateTime格式转换为Unix时间戳 /// </summary> /// & ...
- datetime,Timestamp和datetime64之间转换
引入工具包 import datetime import numpy as np import pandas as pd 总览 from IPython.display import Image fr ...
- Mysql - 如何决定用 datetime、timestamp、int 哪种类型存储时间戳?
背景 数据表都很可能会有一两个字段需要保存日期时间数据,那应该用什么 Mysql 类型来保存呢? 前面讲过 datetime.timestamp.int 的方式来保存日期时间 如何存储 10位.13位 ...
- DateTime与timeStamp的转换
DateTime转换为timeStamp: DateTime dt = DateTime.Now; DateTime startTime = TimeZone.CurrentTi ...
- .net 时间戳互相转换(精确到毫秒)
这里记录一个时间戳的互相转换方法,网上都找了,基本都没有精确到毫秒,我的这个基本可以满足精确到毫秒的级别,代码如下: /// <summary> /// Unix时间戳转换为DateTim ...
- Sublime Text3时间戳查看转换插件的开发
平常配置表中,经常需要用到时间配置,比如活动开始结束.从可读性上,我们喜欢2017-04-27 17:00:00,从程序角度,我们喜欢用1493283600.前者是包含时区概念的,而后者市区无关,所以 ...
随机推荐
- openerp修改logo和title
LOGO的修改addons\web\static\src\img下面的:favicon.ico文件和logo2.png \addons\web\static\src\xml\base.xml#82 l ...
- JQuery URL的GET参数值获取方法
// jQuery url get parameters function [获取URL的GET参数值] // <code> // var GET = $.urlGet(); //获取UR ...
- onkeydown-onkeypress-onkeyup
CreateTime--2016年12月17日22:28:36Author:Marydononkeydown.onkeypress和onkeyup参考链接:http://www.jb51.net/ ...
- eclipse 如何运行mavenWeb项目
1.使用maven 首先,在eclipse中,使用maven对项目进行打包: 其次,将项目发布到Tomcat服务器上 说明: demo_WebService2-0.0.1-SNAPSHOT文件夹存 ...
- PLY格式介绍
PLY是一种电脑档案格式,全名为多边形档案(Polygon File Format)或 斯坦福三角形档案(Stanford Triangle Format). 史丹佛大学的 The Digital ...
- Go 1.5keyword搜索文件夹、文件、文件内容_修复一个小BUG
package main import ( "bufio" "bytes" "flag" "fmt" "io& ...
- xargs详解
一.场景 这个命令是错误的 find ./ -perm +700 |ls -l 这样才是正确的 find ./ -perm +700 |xargs ls -l 二.用法 [root@localhos ...
- IE浏览器中,设置指定程序查看源文件
第一步: 先按Ctrl+R,输入regedit进入注册表 依次找到 "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explo ...
- iOS - PairProgramming 结对编程
1.PairProgramming 结对编程(Pair-Programming)可能是近年来最为流行的编程方式.所谓结对编程,也就是两个人写一个程序,其中,一个人叫 Driver,另一个人叫 Obse ...
- python学习笔记——multiprocessing 多进程组件 Pipe管道
进程间通信(IPC InterProcess Communication)是值在不同进程间传播或交换信息. IPC通过有管道(无名管道 和 有名 / 命名管道).消息队列.共享存储 / 内容.信号量. ...