惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - oscar_expansion

test - oscar_expansion - 博客园 WebPart Templates for VS.NET下载地址链接 易用性就这三条原则 将html源代码规范化,转换成XSL代码的asp工具(宝玉师傅) C#入门代码集 - oscar_expansion - 博客园 C#.Net函数和方法集 ASP.NET页面间的传值的几种方法 认识ASP.NET配置文件Web.config ASP Forum2.0学习笔记之二---了解Master Pages库 《Asp.Net Forums2.0深入分析》之 Asp.Net Forums是如何实现代码分离和换皮肤的 《Asp.Net Forums2.0深入分析》序(宝玉) Asp.Net Forums2配置文件(web.config)简要说明 URL欺骗之以假乱真 AspNetForums中对于用户权限....... ASP.NET Forums 2.0 新手安装指南 论坛在线人员的一个显示过程(转自ASP.NET Forums 官方中文网站) 彻底删除一个项目中的源代码管理信息(转自http://blog.joycode.com/) 从网络到现实-------美女写的2004高考作文 美国金牌推销员的成功秘诀 -做人做事箴言录
日期数据转换为字符串再转换为日期时要注意的一点(转自http://blog.joycode.com/)
oscar_expansion · 2005-06-09 · via 博客园 - oscar_expansion

日期数据转换为字符串再转换为日期时要注意的一点

我最近作的一个项目出现了下面这样的bug。(实际代码比这个复杂的多,这里只是演示这个bug的产生。)

DateTime dt1 = new DateTime(2005,5,31,15,31,00); string strDateTime = dt1.ToString("u"); // ...... 一些数据传递操作 DateTime dt2 = DateTime.Parse(strDateTime); int h = dt2.Hour;

DateTime类型的变量被转换成字符串,然后这个字符串又到处传递,走了很复杂的路,在接受方接受到这个字符串后,并再转换为DateTime格式,这时候两个时间的小时数不一样了。

上面演示中,dt1的 Hour 是 15 ,dt2 的 Hour 是 23。 进而造成我所碰到的这个bug。

解决方法,

DateTime dt1 = new DateTime(2005,5,31,15,31,00); string strDateTime = dt1.ToString("u"); DateTime dt2 = DateTime.Parse(strDateTime, null,
System.Globalization.DateTimeStyles.AdjustToUniversal);
int h = dt2.Hour;

或者

DateTime dt1 = new DateTime(2005,5,31,15,31,00); string strDateTime = dt1.ToString(); DateTime dt2 = DateTime.Parse(strDateTime); int h = dt2.Hour;

我猜想原因应该是:
我本机日期设置是某种格式,我转换为字符串的时候,用了不是我本机的这种格式(使用了UniversalSortableDateTimePattern 这种格式( using the format for universal time display )),而转换回去的时候,确用了本机默认格式,就造成了这个问题。解决方法就是通用一个识别格式。