前段时间项目中涉及到了MySql和MsSql数据类型之间的转换,最近又在研究新浪微博的API,涉及到了带有时区的GMT时间类型的转换,所以,特记录于此,以备日后查询。

一:UNIX时间戳与datetime时间之间的转换

1. 将Unix时间戳转换为DateTime类型时间

方法一:

        /// <summary>
/// 将Unix时间戳转换为DateTime类型时间
/// </summary>
/// http://www.cnblogs.com/babycool
/// <param name="d"></param>
/// <returns></returns>
public static DateTime ConvertIntToDateTime(double d)
{
System.DateTime time = System.DateTime.MinValue;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
time = startTime.AddSeconds(d);
return time;
}

方法二:

/// <summary>
/// 将Unix时间戳转换为DateTime类型时间
/// </summary>
/// http://www.cnblogs.com/babycool
/// <param name="time"></param>
/// <returns></returns>
static DateTime ConvIntToDateTime(long time)
{
DateTime timeStamp = new DateTime(1970, 1, 1); //得到1970年的时间戳
long t = (time + 8 * 60 * 60) * 10000000 + timeStamp.Ticks;
DateTime dt = new DateTime(t);
return dt;
}

2.在SQL Server Management Studio 中查询并转换:

--将Unix时间戳转换为dateline类型
select top 10 DATEADD(SS,regdate,'1970-01-01 00:00:00') from dbo.uc_members

3. 将DateTime时间格式转换为Unix时间戳格式

/// <summary>
/// 将DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// http://www.cnblogs.com/babycool
/// <param name="time"></param>
/// <returns></returns>
public static double ConvertDateTimeToInt(System.DateTime time)
{
double intResult = 0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (time - startTime).TotalSeconds;
return intResult;
}

二:将新浪微博中带有时区的GMT时间转换为DateTime

/// <summary>
/// 将新浪微博中带有时区的GMT时间转换为DateTime
/// </summary>
/// http://www.cnblogs.com/babycool
/// <param name="dateString">微博时间字符串</param>
/// <returns>DateTime</returns>
public static DateTime ParseUTCDate(string dateString)
{
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture; DateTime dt = DateTime.ParseExact(dateString, "ddd MMM dd HH:mm:ss zzz yyyy", provider); return dt;
}

三:转换效果:

相应代码:

            //新浪微博返回的时间是带有时区的GMT时间,转换为datetime类型时间格式:

            // GMT时间: Tue May 31 17:46:55 +0800 2011
Response.Write("将GMT时间转换为datetime类型时间:");
Response.Write(ParseUTCDate("Tue May 31 17:46:55 +0800 2011").ToString());
Response.Write("<br/>"); //UNIX时间戳 1176686120
Response.Write("将UNIX时间戳转换为datetime类型时间:");
Response.Write(ConvertIntToDateTime(1176686120));
Response.Write("<br/>");
Response.Write("方法二:");
Response.Write(ConvIntToDateTime(1176686120));
Response.Write("<br/>");
Response.Write("将datetime类型时间转换为UNIX时间戳:");
Response.Write(ConvertDateTimeToInt(ConvertIntToDateTime(1176686120)));
Response.Write("<br/>");
本文转自 酷小孩 博客园博客,原文链接:http://www.cnblogs.com/babycool/archive/2013/06/13/3134471.html,如需转载请自行联系原作者

[开发笔记]-unix时间戳、GMT时间与datetime类型时间之前的转换的更多相关文章

  1. Java开发笔记(四十四)本地日期时间与字符串的互相转换

    之前介绍Calendar的时候,提到日历实例无法直接输出格式化后的时间字符串,必须先把Calendar类型转换成Date类型,再通过格式化工具SimpleDateFormat获得字符串.而日期时间的格 ...

  2. Java开发笔记(二十七)数值包装类型

    方法的出现缘起优化代码结构,但它的意义并不局限于此,正因为有了方法定义,编程语言才更像一门能解决实际问题的工具,而不仅仅是只能用于加减乘除的计算器.在数学的发展过程中,为了表示四则运算,人们创造了加减 ...

  3. Java开发笔记(三十三)字符包装类型

    正如整型int有对应的包装整型Integer那样,字符型char也有对应的包装字符型Character.初始化字符包装变量也有三种方式,分别是:直接用等号赋值.调用包装类型的valueOf方法.使用关 ...

  4. 利用UNIX时间戳来计算ASP的在线时间

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><!DOCTYPE html PUBLIC "-/ ...

  5. 开发工具-Unix时间戳转换

    更新日志 2022年6月10日 初始化链接. https://toolb.cn/timestamp

  6. iOS开发笔记系列-基础3(多态、动态类型和动态绑定)

    多态:相同的名称,不同的类 使不同的类共享相同方法名称的能力成为多态.它让你可以开发一组类,这组类中的每一个类都能响应相同的方法名.每个类的定义都封装了响应特定方法所需要的代码,这使得它独立于其他的类 ...

  7. Java开发笔记(五)数值变量的类型

    如今个人电脑的配置越来越高,内存和硬盘的容量大小都是以G为单位,而1G=1024M=1024*1024K=1024*1024*1024字节.不过在PC的早期发展阶段,电脑的存储空间却是十分有限的,像2 ...

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

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

  9. Unix时间戳与C# DateTime时间类型互换

    Unix时间戳最小单位是秒,开始时间为格林威治标准时间1970-01-01 00:00:00ConvertIntDateTime方法的基本思路是通过获取本地时区表示Unixk开始时间,加上Unix时间 ...

随机推荐

  1. Mysql数据库卸载

    Mysql数据库卸载的操作流程(Windows10): 1.停止mysql的所有服务 方法一:此电脑——管理——服务中查找到所有Mysql的服务,并停止. 方法二:cmd——net stop mysq ...

  2. zabbix模板的自动发现规则(ldd)实现被监控项自动发现

    zabbix模板的自动发现规则(ldd)实现被监控项自动发现 自动发现规则(ldd)用途说明 在zabbix自带的linux模板的自动发现规则中,有一个Mounted filesystem disco ...

  3. django发送邮件的坑

    django发送邮件的坑 配置django发邮件的时候本地发送时好好的,但是放到阿里云的服务器上却不能发送. 经过一系列的排查后终于发现是阿里云把25端口给封了. 后来改用smtps的方式发送,更改d ...

  4. SQLAlchemy查询

    SQLAlchemy查询 结果查询: from databases.wechat import User from config import session def search(): result ...

  5. linux之进程管理(二)

    一.查看进程 ps   aux 查看系统所有的进程数据 ps   -lA 查看所有系统的数据 ps   axjf 连同部分进程树状态 ps参数 -A   显示所有进程,等效 -e -a   不与ter ...

  6. Python设计模式(3)-工厂方法模式

    # coding=utf-8 #定义一个用于创建对象的接口,让子类决定实例化哪一个类 class DbManager: def __init__(self): pass def operate_db( ...

  7. CentOS安装C函数库的man帮助

    安装linux可能没有安装C的man帮助, 像我安装时选择的是最小化安装就没有, 网上的大多是ubunu的安装方式,或者是C++的man帮助, 都不适合,那么CentOS安装C man手册的方法就是: ...

  8. golang trace 分析 简例

    今天,通过一个例子,一方面熟悉trace在自定义范围内的分析,另一方面golang 在协程调度策略上的浅析. Show Code // trace_example.go package main im ...

  9. getline()和get()的使用区别

    一.getline和get()的使用区别: 首先这两个函数都读取下一行输入,直到到达换行符:但是getline()函数会丢弃换行符,而get()将换行符保留在输入序列中 二.getline()函数的使 ...

  10. Python-气象-大气科学-可视化绘图系列(一)——利用xarray读取netCDF文件并画图(代码+示例)

    本文原创链接:https:////www.cnblogs.com/zhanling/p/12192978.html 1 import numpy as np import xarray as xr i ...