DateTime.ToShortDateToSting()  
Author Message
Tryin2Bgood





PostPosted: Visual C# Language, DateTime.ToShortDateToSting() Top

protected DateTime _CreatedOn = Convert.ToDateTime(DateTime.Now.ToShortDateString());

I thought this method return the date in this format 10/12/2005 but mine is giving me the time too....I just want the date and thats all...is there something wrong with how I am doing this




Visual C#1  
 
 
ahmedilyas





PostPosted: Visual C# Language, DateTime.ToShortDateToSting() Top

to just get the date, try specifying the format in the ToString() overload:

DateTime.Now.ToString("dd/MM/yyyy");

http://msdn2.microsoft.com/en-gb/library/zdtaw1bw.aspx

http://msdn2.microsoft.com/en-gb/library/8kb3ddd4.aspx



 
 
boban.s





PostPosted: Visual C# Language, DateTime.ToShortDateToSting() Top

_CreatedOn will always have time, because it is of type DateTime. If you want to get current date and time to be 12:00:00 AM then you need this:

protected DateTime _CreatedOn = DateTime.Today;



 
 
Tryin2Bgood





PostPosted: Visual C# Language, DateTime.ToShortDateToSting() Top

_Vendor.CreatedOn = Convert.ToDateTime(lblCreatedOnValue.Text.ToString("MM/dd/yyyy")

I get this error when doing it this way

The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments
cannot convert from 'string' to 'System.IFormatProvider'



 
 
ahmedilyas





PostPosted: Visual C# Language, DateTime.ToShortDateToSting() Top

should be:

_Vendor.CreatedOn = Convert.ToDateTime(lblCreatedOnValue.Text).ToString("MM/dd/yyyy");



 
 
Tryin2Bgood





PostPosted: Visual C# Language, DateTime.ToShortDateToSting() Top

Nope that gives me an error of Cannot conver string to System.DateTime......dang...

 
 
ahmedilyas





PostPosted: Visual C# Language, DateTime.ToShortDateToSting() Top

your CreatedOn object is a datetime object, not a string object. so take out the ToString() at the end and only have this:

_Vendor.CreatedOn = Convert.ToDateTime(lblCreatedOnValue.Text);

 

then when it comes to showing the CreatedOn property in a string format, just do:

.CreatedOn.ToString("MM/dd/yyyy");