Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Member
228 Points
52 Posts
Apr 24, 2012 04:28 AM|LINK
Hi andre
Suppose,The date you read from database is with the format "2012-04-23 00:00:00.000".
just take the variable of datatype DateTime and store this value in that variable.Then when you are reading the date you can change the
format as per your convinence.
string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString; SqlConnection conn = new SqlConnection(strcon); conn.Open();
string strdate="select date from tablename";
SqlCommand query = new SqlCommand(strdate, conn);
String date=cmd.ExecuteScalar();
DateTime datetime = Convert.ToDateTime(date.ToString());
String datetime1 = datetime .ToShortDateString(); Char[] sep = { '/' }; String[] arr = datetime1 .Split(sep);
String mmddyyyyformat=arr[1].ToString() + "-" + arr[0].ToString() + "-" + arr[2].ToString();
string ddmmyyyyformat=arr[0].ToString() + "-" + arr[1].ToString() + "-" + arr[2].ToString();
Response.Write(mmddyyyyformat.ToString());
Response.Write(ddmmyyyyformat.ToString());
Here
arr[0].ToString() refers to date part of the complete date.
arr[1].ToString() refers to month part of the complete date.
arr[2].ToString() refers to year part of the complete date.
Hope this will help you.
poojajoon
Member
228 Points
52 Posts
Re: Date format always changes
Apr 24, 2012 04:28 AM|LINK
Hi andre
Suppose,The date you read from database is with the format "2012-04-23 00:00:00.000".
just take the variable of datatype DateTime and store this value in that variable.Then when you are reading the date you can change the
format as per your convinence.
string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
string strdate="select date from tablename";
SqlCommand query = new SqlCommand(strdate, conn);
String date=cmd.ExecuteScalar();
DateTime datetime = Convert.ToDateTime(date.ToString());
String datetime1 = datetime .ToShortDateString();
Char[] sep = { '/' };
String[] arr = datetime1 .Split(sep);
String mmddyyyyformat=arr[1].ToString() + "-" + arr[0].ToString() + "-" + arr[2].ToString();
string ddmmyyyyformat=arr[0].ToString() + "-" + arr[1].ToString() + "-" + arr[2].ToString();
Response.Write(mmddyyyyformat.ToString());
Response.Write(ddmmyyyyformat.ToString());
Here
arr[0].ToString() refers to date part of the complete date.
arr[1].ToString() refers to month part of the complete date.
arr[2].ToString() refers to year part of the complete date.
Hope this will help you.