如何将字符串日期更改为datetime [英] how to change string date to datetime

查看:88
本文介绍了如何将字符串日期更改为datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们!



我从数据库中获取日期。在数据库日期存储为文本(字符串)现在我想使用此文本,只需要从此文本中获取日期和月份。



数据库日期如下24/12/2013​​



现在只需要一个月和一天我该怎么办? c#



i使用了以下很多东西:



 Thread.CurrentThread.CurrentCulture =  new  CultureInfo( 烯-GB)ds1.Ta; 
DateTime datadate = new DateTime(Convert.ToInt32(bles [ 0 ]。行[< span class =code-digit> 0
] birthdate]。ToString( ).Substring( 2 2 )));





2.

 DateTime datadate =  new  DateTime( Convert.ToDateTime(ds1.Tables [ 0 ]。行[ 0 ] [  birthdate]。ToString(),  MM / DD / YY HH:MM:SS TT,System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat); 



< br $> b $ b 3.



  string  bdate = ds1.Tables [ 0 ]。行[ 0 ] [  birthdate]。ToString( ); 
DateTime dt = Convert.ToDateTime(bdate);





并使用更多方法来做但不成功!



请帮助我!

解决方案

尝试



 DateTime.ParseExact(dateString, MMMM dd,System.Globalization.CultureInfo.InvariantCulture); 


HI
试试这个..





  var  datetime = DateTime.ParseExact(  24/12/2013  dd / MM / yyyy,System.Globalization.CultureInfo.InvariantCulture); 
string month = datetime.ToString(< span class =code-string> MM); // 12
string day = datetime.ToString( dd); // 24


尝试这和您的数据集一起修改textbox.text: -

  string  strDay = Convert.ToDateTime(txtDt。文本).ToString(  dd); 
string Month = Convert.ToDateTime(txtDt.Text).ToString( MM);


hi friends!

i fetched date from database. in the database date is store as text(string) now i want to use this text and only take day and month from this text.

database date like this "24/12/2013"

now want only month and day from it how can i do i c#?

i used many things like below :

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB")ds1.Ta;
DateTime datadate = new DateTime(Convert.ToInt32(bles[0].Rows[0]"birthdate"].ToString().Substring(2, 2)));



2.

DateTime datadate = new DateTime(Convert.ToDateTime(ds1.Tables[0].Rows[0]["birthdate"].ToString(), "MM/DD/YY HH:MM:SS TT", System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);



3.

 string bdate = ds1.Tables[0].Rows[0]["birthdate"].ToString();
DateTime dt = Convert.ToDateTime(bdate); 



and used more method to do it but not succeed!

plz help me!

解决方案

TRY

DateTime.ParseExact(dateString, MMMM dd", System.Globalization.CultureInfo.InvariantCulture);


HI Try this..


var datetime = DateTime.ParseExact("24/12/2013", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    string month  = datetime.ToString("MM"); // 12
      string day  = datetime.ToString("dd"); //24


try this and modify textbox.text with your dataset:-

string strDay = Convert.ToDateTime(txtDt.Text).ToString("dd");
 string Month = Convert.ToDateTime(txtDt.Text).ToString("MM");


这篇关于如何将字符串日期更改为datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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