Click here to Skip to main content
15,915,509 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have DateOfBirth in '1986-03-24T00:00:00' format but I want this as'1986-03-24'
format only.
I don't know how to convert datetime to date?

Thanks in advance.
Posted
Updated 11-Oct-18 4:56am
Comments
Rachana Patel 18-May-12 13:36pm    
I can not use below solution becuase the requirement is like, I have text file and I am converting text file into xml file in my xsd datatype for DOB is date , and I cannot change it, while reading txt file takig datetime in string format, so need to convert because datatype in xsd is date I only want date part.
Philippe Mori 23-Aug-14 9:07am    
In XML date and time format are to be written in the format according to the standard. In your text file, the format is known. Thus the only sensible solution is to correctly parse the date in the text file into a DateTime object and the properly write it in XML file using XML expected format as you want your XML to properly works with XML serailization or when importing XML into Excel or any other use. Thus, your question is somewhat irrelevant as their is only one way to do it correctly and all required information already existing in MSDN documentation.

By the wayy, there are no Date object in .NET. In some case, you can use .ToDate() but it only truncate valuee tomidnight. In some situation (don't know if it apply for XML serialization], the might exist an attribute to specify the format as it would be the case in ASP.NET MVC or DateTime control.
Philippe Mori 23-Aug-14 9:16am    
Where is the source code for parsing date in text file and writting it in XML? We have to know what you are doing first to give the best possible answer.

 
Share this answer
 
v3
Comments
Rachana Patel 18-May-12 9:50am    
I want to format the date because in my xsd DOB's datatype is date only.
Prasad_Kulkarni 19-Jul-12 1:21am    
My 5!
Try this:
C#
DateTime dt = DateTime.ParseExact(yourDatetime, "yyyy/MM/DD",                                   CultureInfo.InvariantCulture);


Please refer: Formatting a DateTime for display - format string description[^]
 
Share this answer
 
v2
In case your initial value is in DateTime format then the ToString method of DateTime structure can be used with the InvariantCulture DateFormat with the required pattern as shown below:
C#
DateTime dateVal = new DateTime(1986,03,24);
Console.WriteLine (dateVal.ToString());
Console.WriteLine (dateVal.ToString("yyyy-MM-dd",
	System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat));

//Output
//3/24/1986 12:00:00 AM
//1986-03-24
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-May-12 14:06pm    
Right, a 5.
--SA
VJ Reddy 18-May-12 20:25pm    
Thank you, SA :)
Hi ,
Check this
C#
DateTime dat = Convert.ToDateTime("1986-03-24T00:00:00");
Label2.Text=  dat.ToString("yyyy-MM-dd");

Best Regards
M.Mitwalli
 
Share this answer
 
if u using datetime picker then set the Format property of that.
 
Share this answer
 
C#
string dt = DateTime.Now.ToString("MM-dd-yyyy hh:mm:ss tt");
 
Share this answer
 
v3
Create a Function and then call it from where you need to cast a dateTime instance to short date format.

public static void ParseStringToDate(string date, out DateTime responseDate)
{
string[] dateformats = new string[] { "yyyyMMdd", "yyyy/MM/dd", "yyyy-MM-dd", "M/dd/yy" };
if (!DateTime.TryParseExact(date, dateformats, CultureInfo.InvariantCulture, DateTimeStyles.None, out responseDate))
{
   throw new FormatException("El formato de la fecha  no es válido");
}
}
//Then you call this function. 

DateTime DateTimeValue = DateTime.Now;
DateTime Fecha = item.FechaCita;
clsUtils.ParseStringToDate(DateTimeValue.ToString(), out Fecha);
 
Share this answer
 
v2
Comments
CHill60 11-Oct-18 11:06am    
Seems a bit of overkill when all you actually need to use is ToString()

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