I want to trip/remove seconds from date time. I checked some solution but showing solution using format string like
DateTime datetime = DateTime.UtcNow;
To get a local time you can use:
var dt = DateTime.Now;
dt = dt.AddTicks(-(dt.Ticks % (60 * 10_000_000)));
DateTime
is actually stored separately as a Date
and a TimeOfDay
. We can easily re-initialize the date without including the seconds in the TimeSpan
initialization. This also ensures any leftover milliseconds are removed.
date = date.Date + new TimeSpan(date.TimeOfDay.Hours, date.TimeOfDay.Minutes, 0);