Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friend,
I wan to get date from a variable but when it return value for me, doesn't have a true form.
C#
public static List<business> ContactSelectAll()
       {
           DataTable dtb = DataAccess.ContactSelectAll();
           List<business> result = new List<business>();
           foreach (DataRow record in dtb.Rows)
           {
               Business bus = new Business();
               bus.Id = Convert.ToInt32(record["Id"]);
               bus.Name = Convert.ToString(record["Name"]);
               bus.Family = Convert.ToString(record["Family"]);
               bus.Address = Convert.ToString(record["Address1"]);
               bus.PhoneNumber = Convert.ToString(record["PhoneNumber"]);

               DateTime birth = new DateTime();
               bus.BirthDate = Convert.ToString(birth.Date.ToString("d/mm/yyyy"));

               bus.EmailAddress = Convert.ToString(record["EmailAddress"]);
               bus.FacebookAddress = Convert.ToString(record["Facebook"]);

               result.Add(bus);
           }
           return result;
       }
Posted
Updated 25-Sep-13 0:01am
v2
Comments
[no name] 25-Sep-13 5:51am    
"dd/MM/yyyy"
Elham.Deljooei 25-Sep-13 5:58am    
I used it but It doesn't work truly.
For example it have to return 02/02/2000 but it return 01/00/0001
CPallini 25-Sep-13 6:05am    
You did not initialize 'birt'.
Maciej Los 25-Sep-13 6:09am    
Why don't you post as a comment? It's an answer!
My virtual 5!
CPallini 25-Sep-13 6:16am    
My real thanks.

The DateTime.ToString method is case sensitive: "MM" is not the same as "mm".
The first one is the Month, with a leading zero, the second is the minute with a leading zero.
If you want the date, you need to look at what ThePhantomUpvoter said, and change your "d/mm/yyyy" to "dd/MM/yyyy"

There is a list of format codes here which might help: Formatting a DateTime for display - format string description[^]
 
Share this answer
 
Comments
Volynsky Alex 25-Sep-13 11:11am    
Nice!
Convert.ToDateTime(birth.Date).ToString("dd/MM/yyyy"));
 
Share this answer
 
Comments
Volynsky Alex 25-Sep-13 11:10am    
Fine!
Use like this

DateTime birth = DateTime.Today;
bus.BirthDate = birth.ToString("DD/mm/YYYY");
 
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