Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a short date in a DateTime object

Tags:

c#

datetime

I'm trying to store a shortened date (mm/dd/yyyy) into a DateTime object. The following code below is what I am currently trying to do; this includes the time (12:00:00 AM) which I do not want :(

DateTime goodDateHolder = Convert.ToDateTime(DateTime.Now.ToShortDateString());

Result will be 10/19/2009 12:00:00 AM

like image 670
contactmatt Avatar asked Oct 20 '09 00:10

contactmatt


People also ask

How do you shorten dates?

1. In most countries of the world, dates are written in order DAY/MONTH/YEAR, e.g., 4 July 1999, abbreviated 4/7/1999. 2. In eastern Asia, however, dates are written in order YEAR/MONTH/DAY: 1999 July 4, abbreviated 1999/7/4.

How can I get only the date from datetime format?

ToString() − One more way to get the date from DateTime is using ToString() extension method. The advantage of using ToString() extension method is that we can specify the format of the date that we want to fetch. DateTime. Date − will also remove the time from the DateTime and provides us the Date only.

What is short date string?

The string returned by the ToShortDateString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo. ShortDatePattern property. For example, for the en-US culture, the standard short date pattern is "M/d/yyyy"; for the de-DE culture, it is "dd.


1 Answers

You only have two options in this situation.

1) Ignore the time part of the value.

2) Create a wrapper class.

Personally, I am inclined to use option 1.

like image 50
ChaosPandion Avatar answered Nov 07 '22 20:11

ChaosPandion