可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm trying to create a string from the DateTime object which yields the format mm:dd:yyyy
.
Conventionally the DateTime
object comes as mm:dd:yyyy hrs:min:sec AM/PM
.
Is there a way to quickly remove the hrs:min:sec AM/PM
portion of the DateTime so that when I convert it ToString()
it will only result in mm:dd:yyyy
?
回答1:
To answer your question, no - you would have to store it in a different type. The most simple choice is to use a string.
string date = dateTime.ToString("MM:dd:yyyy");
However I'd also strongly advise against storing dates internally in your program as strings. This will make it difficult to do any calculations or comparisons on them. Furthermore I'd advise you against forcing a specific culture for your date representation as it means your application probably won't work as expected in other cultures than yours.
A slightly more sophisticated approach is to create a custom class which overrides ToString. I'd also avoid this though, because it will still be difficult to use your type with the standard library functions. You will have to convert back and forth all the time.
Just leave it as a DateTime and do the conversion to string only in the presentation layer. You can use DateTime.ToShortDateString
to print a user friendly culture aware string.
回答2:
datetime DateWithTimeNoSeconds =
DateTime.Now.Date.AddHours(DateTime.Now.Hour).AddMinutes(DateTime.Now.Minute);
This gets the current date & time's date and adds hours and minutes.
回答3:
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());
// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
// The example displays the following output to the console:
// 6/1/2008 7:47:00 AM
// 6/1/2008
// 6/1/2008 12:00 AM
// 06/01/2008 00:00
http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx
回答4:
Usually, I am using DateTime.ToShortDateString() to convert in a Culture
-aware manner to a string.
This way, you can format it to date only respecting the current formatting of the culture set to the current thread.
回答5:
While in most cases I agree with Mark Byers, I had a situation where I needed to store a date time that was only ever granular to the hour. Storing minutes and seconds would not only be superfluous, but also inaccurate. The user simply selected a date and hour, so while the date and hour would be user selected, the minutes and seconds would be set to whatever the current time was.
Removing minutes and seconds is very easy in this case. Here is the code:
scheduledDate = scheduledDate.AddMinutes(
scheduledDate.Minute * -1).AddSeconds(
scheduledDate.Second * -1);
Then I store it in the DB as a full date time, with minutes and seconds always 0.
回答6:
About DateTime.AddMinutes
(or seconds or hours) if you want to REMOVE instead of adding just add a negative number!
回答7:
Easy way, probably not the best but
DateTime dt = new DateTime();
dt = DateTime.Now;
string sdate = dt.ToShortDateString();
dt = DateTime.Parse(sdate);
Or for short
var dt = DateTime.Parse(DateTime.Now.ToShortDateString());
回答8:
If you want to remove hours, seconds, milliseconds (remove as reset to 0) from a DateTime
object without converting to a string, you can use the Date
property.
// Get date-only portion of date, without its time.
var date = DateTime.Now.Date;
The example below uses the Date property to extract the date component of a DateTime value with its time component set to zero (or 0:00:00, or midnight).
msdn
回答9:
Something like this for date and time without seconds:
private DateTime _startTime;
public DateTime StartTime
{
get { return _startTime; }
set
{
_startTime = value.AddSeconds(-value.Second)
.AddMilliseconds(-value.Millisecond);
}
}