DateTime dt = DateTime.Now dt.Date is created to 31.10.2012 00:00:00 .it is created to dd.mm.yyyy format but i need dd/mm/yyyy. Can i use: return new DateTime(d.Year, d.Month
DateTime
, numeric types and most other types do not store their values in a formatted way. Rather they store their data using a binary representation. If you want to display this data to the user, you must convert it to a string. This conversion involves formatting the data.
string formattedDate = DateTime.Now.ToString("dd/MM/yyyy",
CultureInfo.InvariantCulture);
Or
Console.WriteLine("Date = {0:dd/MM/yyyy}", DateTime.Now);
Console.WriteLine
converts the date into a string in order to write it to the console.
System.DateTime
does not have any format. You can view its string representation in format.
Try this
Console.WriteLine(DateTime.Now.Date.ToString("dd'/'MM'/'yyyy"));
Just the way to convert to string, DateTime
itself has no format:
var result = DateTime.Now.Date
.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
In you case you can simply use :
dt.ToString("dd/MM/yyyy");
Anyway there al the string format you can use with DateTime : Here.
The DateTime
struct doesn't store any formatting information internally. If you want to output the DateTime
instance as a formatted string, you just need to call ToString() with the proper format string:
var date = DateTime.Now;
var formattedString = date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
If you need more information on exactly which specifiers to use in your format string, check out:
MSDN - Custom Date and Time Format Strings
DateTime structure always has the Date and Time stored in it. If you need to extract the date alone as text you can do the following.
var date = DateTime.Now.ToString("d");
Console.WriteLine(date);
This will print the date as in the format as specified by the culture set in the system. The list of standard datetime format strings supported by dotnet framework can be found here