Reputation: 1397
In my project, I am in need of creating a DateTime
object in below format.
"MM/dd/yy hh:mm tt" (without seconds and the year in two digit format)
When I am creating a date time object with a string (like "7/12/12 04:50 AM"
), the created date time object is in the format of complete date time structure. (7/12/2012 04:50:00 AM)
Is there any way to create date time object (not string format) in particular format?
Upvotes: 1
Views: 881
Reputation: 3584
you can try by converting this to string and give your specified format as follows:
datetime dt=new datetime();
string dateval=dt.tostring("MM/dd/yy hh:mm tt")
thanks.
Upvotes: 0
Reputation: 151586
No, the DateTime
object stores a date and time in a predetermined format.
What you are trying to do really should be done when displaying the date.
Perhaps if you explain the real problem you're trying to solve, someone can suggested a better alternative.
Upvotes: 0
Reputation: 17193
No, It has predefined date and time value (such as the year, month, and day, or the number of ticks).
There is no way other than converting it in string with the format you are in need of.
DateTime dat1 = new DateTime();
// The following method call displays 1/1/0001 12:00:00 AM.
It uses the default DateTime.ToString()
method to display the date and time using the short date and long time patterns.
Upvotes: 2
Reputation: 77295
The DateTime object does not have any format. It has members and those members will have values. If you don't supply those values, they will be 0. The only way you can hide them is to format the DateTime object to a String where those values will not be shown. But in the DateTime object itself, they will still exist.
Upvotes: 1