我怎么能转换成字符串日期时间在.NET? [英] How can I convert a string into datetime in .NET?

查看:261
本文介绍了我怎么能转换成字符串日期时间在.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以转换的日期,如2009年6月17日,下午3时37分ET成的DateTime 变量使用C#?

我已经试过 DateTime.ParseExact ,但我还没有想出使用正确的格式。

解决方案

  //字符串日期时间
 字符串MyString中;
 MyString的=1999-09-01 21:34 PM;
 // MyString的=1999-09-01下午21时34分; //取决于您的区域设置

 日期时间MyDateTime;
 MyDateTime =新的日期时间();
 MyDateTime = DateTime.ParseExact(MyString的,YYYY-MM-DD HH:MM TT,NULL);
 

来源: HTTP://www.$c$cproject.com/ KB / CS / String2DateTime.aspx

修改,以适应您的日期格式为:

  //字符串日期时间
 字符串MyString中;
 MyString的=2009年6月17日,下午3时37分;

 日期时间MyDateTime;
 MyDateTime =新的日期时间();
 MyDateTime = DateTime.ParseExact(MyString的,MMM DD YYYY,HH:MM TT,NULL);
 

How can I convert dates like "Jun 17 2009, 03:37 pm ET" into a DateTime variable using C#?

I have tried DateTime.ParseExact but I haven't figured out the correct format to use.

解决方案

 // String to DateTime
 String MyString;
 MyString = "1999-09-01 21:34 PM";
 //MyString = "1999-09-01 21:34 p.m.";  //Depends on your regional settings

 DateTime MyDateTime;
 MyDateTime = new DateTime();
 MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", null);

Source: http://www.codeproject.com/KB/cs/String2DateTime.aspx

Modified to fit your date format:

 // String to DateTime
 String MyString;
 MyString = "Jun 17 2009, 03:37 pm";

 DateTime MyDateTime;
 MyDateTime = new DateTime();
 MyDateTime = DateTime.ParseExact(MyString, "MMM dd YYYY, HH:mm tt", null);

这篇关于我怎么能转换成字符串日期时间在.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆