Click here to Skip to main content
15,886,714 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Dear Friends,

Good Day to every one of you............

I have a registration page to create the users..In this it has Issue date and Expiry date of user accessing the application.

I have set, Issue date will be the system date and for Expiry date administrator will choose it for the user.

For Expiry date I have a textbox with Ajax Calendar Extender....when i click on submit the expiry date is storing in database as 11/30/2012

But my requirement is.....

I want to display date as dd/MM/yyyy Format using label...I have used LABEL.

This is my code....Please help.

C#
protected void Page_Load(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(_connString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "select datediff(dd,getdate(),ExpiryDate) as Edays, CONVERT(varchar, expirydate, 101) as ExpiryDates from dbo.UserAccounts where UserName='" + Session["UserName"].ToString() + "'";


        using (con)
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {

            


               // lblExpiryDisplay.Text = DateTime.Parse(reader["ExpiryDates"].ToString()).ToString("dd/MM/yyyy");

                lblExpiryDisplay.Text = "Your Password will Expire on " + reader["ExpiryDates"].ToString() + " , and the Left Over days are " + reader["Edays"].ToString();


            }

           
        }
        con.Close();
        }
Posted
Updated 18-Nov-12 20:01pm
v2

Quote:
lblExpiryDisplay.Text = "Your Password will Expire on " + reader["ExpiryDates"].ToString() + " , and the Left Over days are " + reader["Edays"].ToString();


Try this:
C#
lblExpiryDisplay.Text = "Your Password will Expire on " + Convert.ToDateTime(reader["ExpiryDates"]).ToString("dd/MM/yyyy") + " , and the Left Over days are " + reader["Edays"].ToString();
 
Share this answer
 
To get date in dd/MM/yyyy format use
SQL
CONVERT(VARCHAR,expirydate,103)
in your query

Refer
How to format datetime & date in Sql Server[^]
 
Share this answer
 
Comments
Software Engineer 892 19-Nov-12 2:24am    
Thanx, 103 is working
__TR__ 19-Nov-12 2:25am    
You are welcome :)
member60 19-Nov-12 22:23pm    
my 5!
__TR__ 20-Nov-12 2:53am    
Thank you.
Try the below one

CONVERT(VARCHAR ,expiry date ,103)

syntax: convert(datatype ,column-name,style number)

ex:

in the above syntax you can specify your style number.

style numbers are valid from 1 to 9 , 100 to 110, 120 to 126

based on your expected output you can pass the stylish number.
 
Share this answer
 

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