陋室铭
永远也不要停下学习的脚步(大道至简至易)

posts - 2169,comments - 570,views - 413万
复制代码
复制代码
1 /// <summary>  
2 /// 本地时间转成GMT时间  
3 /// </summary>  
4  public static string ToGMTString(DateTime dt)  
5  {  
6     return dt.ToUniversalTime().ToString("r");  
7 }  
复制代码
复制代码

string s = ToGMTString(DateTime.Now);
//本地时间为:2016-6-30 10:04:39
//转换后的时间为:Thu, 30 Jun 2016 2:04:39 GMT

复制代码
复制代码
1 /// <summary>  
2 /// 本地时间转成GMT格式的时间  
3 /// </summary>  
4 public static string ToGMTFormat(DateTime dt)  
5 {  
6     return dt.ToString("r") + dt.ToString("zzz").Replace(":", "");  
7 }
复制代码
复制代码

string s = ToGMTFormat(DateTime.Now);
//本地时间为:2016-6-30 10:04:39
//转换后的时间为:Thu, 30 Jun 2016 10:04:39 GMT+0800

复制代码
复制代码
 1 /// <summary>  
 2 /// GMT时间转成本地时间  
 3 /// </summary>  
 4 /// <param name="gmt">字符串形式的GMT时间</param>  
 5 /// <returns></returns>  
 6 public static DateTime GMT2Local(string gmt)  
 7 {  
 8     DateTime dt = DateTime.MinValue;  
 9     try  
10     {  
11         string pattern = "";  
12         if (gmt.IndexOf("+0") != -1)  
13         {  
14             gmt = gmt.Replace("GMT", "");  
15             pattern = "ddd, dd MMM yyyy HH':'mm':'ss zzz";  
16         }  
17         if (gmt.ToUpper().IndexOf("GMT") != -1)  
18         {  
19             pattern = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";  
20         }  
21         if (pattern != "")  
22         {  
23             dt = DateTime.ParseExact(gmt, pattern, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal);  
24             dt = dt.ToLocalTime();  
25         }  
26         else  
27         {  
28             dt = Convert.ToDateTime(gmt);  
29         }  
30     }  
31     catch  
32     {  
33     }  
34     return dt;  
35 }
复制代码
复制代码

DateTime dt1 = GMT2Local("Thu, 30 Jun 2016 02:04:39 GMT"); 
//转换后的dt1为:2016-6-30 10:04:39
DateTime dt2 = GMT2Local("Thu, 30 Jun 2016 10:04:39 GMT+0800");
//转换后的dt2为:2016-6-30 10:04:39

posted on   宏宇  阅读(8548)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2012-10-08 url重写
2008-10-08 C#中将字符串转成 Base64 编码(小技巧)
2007-10-08 xml矢量图:svg的viewBox和vml的coordsize决定的虚坐标系简说
2006-10-08 学习了数据库中text类型的查找
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示