日期格式更改为2014年11月16日,而不是2014年11月16日 [英] date format changing to nov 16 2014 instead of 16, November, 2014

查看:86
本文介绍了日期格式更改为2014年11月16日,而不是2014年11月16日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码。





请协助纠正上述预期的格式







 SqlCmd.Parameters。添加  @ ZDATE_LOAN,System.Data.SqlDbType。 VarChar  12 ); 
SqlCmd.Parameters [ @ ZDATE_LOAN]。Direction = System.Data.ParameterDirection。 输出;







  var  dts = SqlCmd.Parameters [  @ZDATE_LOAN] Value.ToString(); 
txt_Date_Of_Loan.Text = String .Format( {0:dd,MMMM,yyyy},dts);







谢谢

解决方案

如果要在数据库中将Date保存为字符串/ varchar,首先最好将列类型更改为Date或DateTime。然后你可以声明参数如下

 SqlCmd.Parameters.Add(  @ ZDATE_LOAN,System.Data.SqlDbType.DateTime); 



因为你现在有了DateTime参数 SqlCmd。参数[@ ZDATE_LOAN]。值将是DateTime值或null值。你最好首先检查null,然后调用 SqlCmd.Parameters [@ ZDATE_LOAN]。Value.ToString(MMMM,dd,yyyy)取格式化的字符串输出返回日期。



如果你不能改变数据库列类型,那么 SqlCmd.Parameters [@ ZDATE_LOAN]。值字符串值为DateTime。您可以执行字符串操作并构建所需的日期时间字符串,如下所示

  string  inputdate =   16,November,2014; 
string [] dateParts = inputdate.Split(' );
if (dateParts.Length == 3
{
string formatedDateString = string .Format( {0},{1},{2},dateParts [ 1 ]。修剪(), dateParts [ 0 ]。修剪(),dateParts [ 2 ]。修剪());
}



但我建议您先将字符串转换为日期时间,然后再调用所需格式的ToString方法,而不是字符串操作。但是您需要使用DateTime.TryParseExact或DateTime.ParseExact以及您在数据库中存储的正确日期时间格式。例如,如果您以dd,MMMM,yyyy的格式保存日期,那么

 DateTime结果; 
if (DateTime.TryParseExact(str, dd,MMMM,yyyy,CultureInfo.InvariantCulture,DateTimeStyles.None, out result)){
string formatedDateString = result.ToString( MMMM,dd,yyyy) ;
}


帮助自己: DateTime的字符串格式[C#] [ ^ ]

This are the codes.


Please assist to correct the format to the expected above



SqlCmd.Parameters.Add("@ZDATE_LOAN", System.Data.SqlDbType.VarChar, 12);
SqlCmd.Parameters["@ZDATE_LOAN"].Direction = System.Data.ParameterDirection.Output;




var dts = SqlCmd.Parameters["@ZDATE_LOAN"].Value.ToString();
             txt_Date_Of_Loan.Text = String.Format("{0:dd, MMMM, yyyy}", dts);




Thanks

解决方案

if you are saving Date as string/varchar in the database, first you better change the column type to Date or DateTime. then you can declare parameter as below

SqlCmd.Parameters.Add("@ZDATE_LOAN", System.Data.SqlDbType.DateTime);


since you have DateTime parameter now SqlCmd.Parameters["@ZDATE_LOAN"].Value will be a DateTime value or null value. you better check for null first and then call SqlCmd.Parameters["@ZDATE_LOAN"].Value.ToString("MMMM, dd, yyyy") to take the formatted string output of returned date.

if you can't change the database column type, then SqlCmd.Parameters["@ZDATE_LOAN"].Value having string value of DateTime. You can do string manipulation and build the required datetime string like below

string inputdate = "16, November, 2014";
string[] dateParts =inputdate.Split(',');
if(dateParts.Length ==3)
{
   string formatedDateString =string.Format("{0}, {1}, {2}",  dateParts[1].Trim(), dateParts[0].Trim(),dateParts[2].Trim());
}


But rather than string manipulation I recommend you to convert string to datetime first and then call ToString method with the format which needed. But you need to use DateTime.TryParseExact or DateTime.ParseExact with correct date time format which you have store in the database. for example if you save dates in the format of dd, MMMM, yyyy then

DateTime result;
if (DateTime.TryParseExact(str,"dd, MMMM, yyyy",CultureInfo.InvariantCulture,DateTimeStyles.None,out result)){
    string formatedDateString =result.ToString("MMMM, dd, yyyy");
}


Help yourself to this: String Format for DateTime [C#][^]


这篇关于日期格式更改为2014年11月16日,而不是2014年11月16日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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