C # DateTime, Trim without conversion to string

How can I “trim” a property value DateTimein C #?

For example, when I get a date, it has the format "10/1/2010 00:00:00".

How can I “crop” the “Time” 00:00:00 without converting it to a string?

Since I use a type property DateTimeto control this, I do not need to convert to String.

Any help is appreciated.

+3
source share
6 answers
var dt = DateTime.Now;    // 10/1/2010 10:44:24 AM
var dateOnly = dt.Date;   // 10/1/2010 12:00:00 AM
+8
source

, DateTime , - . , Date, Date DateTime. , . .

DateTime now = DateTime.Now;
DateTime today = now.Date;

DateTime now = DateTime.Now;
DateTime today = new DateTime(now.Year, now.Month, now.Day);
+2

, .

 DateTime t = DateTime.Now;
 DateTime t2 = t - new TimeSpan(t.Ticks % TimeSpan.TicksPerDay);

TicksPerHour, TicksPerMinute TicksPerSecond.

+1

DateTime, .ToString() it?

DateTime current = DateTime.Now;
string myTime = current.TimeOfDay.Tostring()

...

0

. DateTime , 00:00:00

.

0

var now = DateTime.Now;
var today = now.Date;

. , timtowtdi phili:

var now = DateTime.Now;
var today = now.Add(-now.TimeOfDay);
0

Source: https://habr.com/ru/post/1767580/


All Articles