I want to ask, I want to read the date from database, but everytime I read from the database for the first time it always shows mmddyyyy format. if I go to then next page and back to first page the format changes to ddmmyyyy (this is what i want). Any solution
to this problem? The date save in database with this format "2012-04-23 00:00:00.000".
Can you please try the following
SQL Convert DateTime operation. If you check the given reference link, you will see that the convert options 3 and 4 will also work for your case just like option 5 in the below example.
andre127
Member
38 Points
30 Posts
Date format always changes
Apr 24, 2012 01:55 AM|LINK
Hi all,
I want to ask, I want to read the date from database, but everytime I read from the database for the first time it always shows mmddyyyy format. if I go to then next page and back to first page the format changes to ddmmyyyy (this is what i want). Any solution to this problem? The date save in database with this format "2012-04-23 00:00:00.000".
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.
eralper
Contributor
6048 Points
971 Posts
Re: Date format always changes
Apr 24, 2012 06:39 AM|LINK
Hi Andre,
Can you please try the following SQL Convert DateTime operation. If you check the given reference link, you will see that the convert options 3 and 4 will also work for your case just like option 5 in the below example.
I hope that helps,
SQL Server 2012