Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
This are the codes.


Please assist to correct the format to the expected above



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



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



Thanks
Posted

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
C#
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
C#
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
C#
DateTime result;
if (DateTime.TryParseExact(str,"dd, MMMM, yyyy",CultureInfo.InvariantCulture,DateTimeStyles.None,out result)){
    string formatedDateString =result.ToString("MMMM, dd, yyyy");
}
 
Share this answer
 
v2
Help yourself to this: String Format for DateTime [C#][^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900