前段时间项目中涉及到了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. PTA | 1014 福尔摩斯的约会 (20分)

    大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm.大侦探很快就明白了,字条上奇 ...

  2. ubuntu上安装lamp环境命令清单

    #install configuration manager sudo apt-get install tasksel #install basic lamp stack sudo tasksel i ...

  3. 在 Array.filter 中正确使用 Async

    本文译自How to use async functions with Array.filter in Javascript - Tamás Sallai. 0. 如何仅保留满足异步条件的元素 在第一 ...

  4. 微信小程序H5预览页面框架

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. JAVA自动化之Junit单元测试框架详解

    一.JUnit概述&配置 1.Junit是什么? Junit是一个Java 编程语言的开源测试框架,用于编写和运行测试.官网 地址:https://junit.org/junit4/ 2.Ma ...

  6. discuz 用户整合 账号整合 ucenter php网站整合discuz用户

    引用:https://www.cnblogs.com/kenkofox/archive/2011/09/18/2180649.html 1.登录后台管理.(在论坛中,用创建论坛的admin账号登陆,然 ...

  7. git rebase解决合并冲突

    git rebase解决合并冲突   记录合并冲突解决方法,使用的git rebase,感觉很好用 1.git rebase 文档 https://git-scm.com/docs/git-rebas ...

  8. Array(数组)对象-->indexOf() 方法

    1.定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置,即下标. 如果没有找到匹配的字符串则返回 -1. 语法: string.indexOf(searchvalue ...

  9. 基于 HTML5 WebGL 的楼宇智能化集成系统(三)

    前言       2018年7月,信息化部印发了<工业互联网平台建设及推广指南>和<工业互联网平台评价方法>,掀起了 工业互联网 的浪潮,并成为热词写入了报告中.同为信息发展下 ...

  10. AJ学IOS(23)UI之控制器管理

    AJ分享,必须精品 控制器以及view的多种创建方式 控制器view的加载 通过storyboard创建 1:先加载storyboard⽂件(Test是storyboard的⽂文件名) UIStory ...