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

In my database table. I have a column always it will be display date as 10/14/2012 02:07:42 PM

I cannot change Date time in my database,

So on my asp.net webpage I want to display it as 14/10/2012 02:07:42 PM
it should be in dd/mm/yyyy format.

on my webpage i took a label as lblUpdate

This is my code.

here TimeLupdate is column name and TimeLastUpdate is Table Name

PageLoad
=========

C#
SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Select TimeLupdate from TimeLastUpdate ";

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

            if (reader.Read())
            {
                lblUpdate.Text = reader["TimeLupdate "].ToString();

                lblUpdate.ForeColor = Color.Red;
            }
}


Please help i want to display it as DD/MM/YYY and time

Thanks in advance.
Posted
Updated 14-Oct-12 23:25pm
v3
Comments
Azher.Cybertech 15-Oct-12 5:56am    
try it
lblUpdate.text = sqlreader["datetodisplay"].tostring("dd/MM/yyyy");

Try this, May it will help you.

C#
Using Globalization;

CultureInfo ci=new CultureInf("gu-IN");

SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Select TimeLupdate from TimeLastUpdate ";
 
        using (con)
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
 
            if (reader.Read())
            {
                lblUpdate.Text = DateTime.ParseExact(reader["TimeLupdate "].ToString(),ci);
 
                lblUpdate.ForeColor = Color.Red;
            }
}


Thanks
 
Share this answer
 
Hi Try this it will work
C#
SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Select TimeLupdate from TimeLastUpdate ";
 
        using (con)
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
 
            if (reader.Read())
            {
  lblUpdate.Text =string.Format("{0:M/d/yyyy}", reader["TimeLupdate"].ToString();
 
                lblUpdate.ForeColor = Color.Red;
            }
}
 
Share this answer
 
Try this:
C#
string date = "10/14/2012";
int year = Convert.ToInt32(date.Split('/')[2]);
int day = Convert.ToInt32(date.Split('/')[1]);
int month = Convert.ToInt32(date.Split('/')[0]);
DateTime dtOld = new DateTime(year, month, day);
string newDate = dtOld.ToString("dd/MM/yyyy hh:mm:ss tt").Replace('-', '/');



--Amit
 
Share this answer
 
C#
CultureInfo info = new CultureInfo("en-US", false);
DateTime Fdob = new DateTime(1900, 1, 1);
String _dateFormat = "dd/MM/yyyy";
string fdate=reader["TimeLupdate "].ToString();
DateTime.TryParseExact(fdate.ToString().Trim(), _dateFormat, info,
                                                                                                          DateTimeStyles.AllowWhiteSpaces, out Fdob))
 
Share this answer
 
You can use the below code for your purpose:

lblUpdate.Text= DateTime.Parse(reader["TimeLupdate "].ToString()
, System.Globalization.CultureInfo.GetCultureInfo("en-gb"))
 
Share this answer
 
Comments
Software Engineer 892 15-Oct-12 4:19am    
@gopal pradhan SIR, its not working.
it throws an error
'System.Globalization.CultureInfo' is a 'type', which is not valid in the given context
Software Engineer 892 15-Oct-12 4:20am    
also i used namespace using System.globalization;
gopal pradhan 15-Oct-12 4:51am    
try this string.Format("{0:dd/MM/yyyy}", YourDate).ToString();
C#
DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("G"));


this will gives you in your required format..
 
Share this answer
 
v2
hi try this code,

C#
SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Select TimeLupdate from TimeLastUpdate ";
 
        using (con)
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
 
            if (reader.Read())
            {
                string readerValue = reader["TimeLupdate "].ToString();
				DateTime tempDateTime = Convert.ToDateTime(readerValue);
				lblUpdate.Text = string.Format("{0:dd/MM/yyyy hh:mm:ss tt}", tempDateTime);
				
 
                lblUpdate.ForeColor = Color.Red;
            }
}


this will display the date time in the required format.
 
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