Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in CustomersBusinessObject i have like this.

C#
interface CustomersInterface

{
C#
DateTime joiningdate
        {
            get;
            set;
        }
}


public class Customers : ICustomersInterface
{
C#
public DateTime joiningdate
        {
            get
            {
                return _date;
            }
            set
            {
                _date = value;
            }
        }


}

in DAL

public List<customers> getcustomers()
{

try
{
List<customers> lstcustomers = new List<customers>();
SqlConnection con = new SqlConnection(constring);
cmd = new SqlCommand("SELECT empid, empname,departmentid, joiningdate, locationid, salary, jobid, Isworking from employee", con);
con.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Customers cst = new Customers();
cst.employeeid = (int)dr[0];
cst.empname = (string)dr[1];
cst.departmentid = (int)dr[2];
cst.joiningdate = (DateTime)dr[3];
cst.locationid = (int)dr[4];
cst.salary = (int)dr[5];
cst.jobid = (int)dr[6];
cst.isworking = (bool)dr[7];
lstcustomers.Add(cst);
}
}
return lstcustomers;
}
catch (Exception ex)
{
throw;
}
here i want to display only date but it is coming datetime
where i should modify please help me
thank you.
Posted

You don't say HOW you are displaying the data, as a simple string, in some sort of date control?

Assuming it is as a string, then you can use

cst.Joiningdate.toString("dd/mm/yyyy")


for example
 
Share this answer
 
Hi,

I think there should no any modification to do in your code. In my opinion you should consider using format to display such...
Example:
C#
DateTime date1 = new DateTime(2008, 4, 10);
Console.WriteLine(date1.ToString("D", 
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008                        
Console.WriteLine(date1.ToString("D", 
CultureInfo.CreateSpecificCulture("pt-BR")));
// Displays quinta-feira, 10 de abril de 2008                        
Console.WriteLine(date1.ToString("D", 
CultureInfo.CreateSpecificCulture("es-MX")));
// Displays jueves, 10 de abril de 2008   


You may refer into this
VB
Standard Date and Time Format Strings

link
 
Share this answer
 
you can declare joiningdate as a string and then cast it to datetime when you are inserting in database.

C#
cst.joiningdate= DateTime.Now.ToShortDateString();

or
C#
cst.joiningdate= dr[3].ToString("mm/dd/yyyy");

Hope this will help you.
 
Share this answer
 
You can set the format for retrieving the date in particular format.


C#
cst.joiningdate= dr[3].ToString("mm/dd/yyyy");
 
Share this answer
 
cst.joiningdate= string.Format("{0:mm/dd/yyyy}",dr[3])
 
Share this answer
 
Hi there..
Just replace your existing code
<pre lang="c#">cst.joiningdate = (DateTime)dr[3];</pre>

with

<pre lang="c#">cst.joiningdate = ((DateTime)dr[3]).ToShortDateString();</pre>

Try it.. All the best.. Accept answer if you like it.. :)
 
Share this answer
 
Comments
sreekanth12 15-Feb-12 1:29am    
but it is giving error cannot implicitly convert type 'string' to System.date.time

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