25

I want to strip/remove seconds from a DateTime. Starting with a full Datetime like:

DateTime datetime = DateTime.UtcNow;

I want to strip the seconds using any inbuilt function or regular expression.

Input: 08/02/2015 09:22:45

Expected result: 08/02/2015 09:22:00

Tom Dufall
  • 651
  • 1
  • 8
  • 20
Deependra Singh
  • 391
  • 1
  • 3
  • 10

9 Answers9

54

You can create a new instance of date with the seconds set to 0.

DateTime a = DateTime.UtcNow;
DateTime b = new DateTime(a.Year, a.Month, a.Day, a.Hour, a.Minute, 0, a.Kind);

Console.WriteLine(a);
Console.WriteLine(b);
shashwat
  • 7,495
  • 8
  • 55
  • 87
Mike Hixson
  • 4,773
  • 1
  • 17
  • 24
  • 1
    For consistency, I suggest you either use DateTime.Now for creating "a", or add "DateTimeKind.Utc" to the end of the line creating "b". Creating a UTC time without adding the timezone is likely to cause some strange results. – Patrick M Oct 31 '18 at 17:41
  • to complete Patrick comment, use the ctor with DataTimeKind, so DateTime b = new DateTime(a.Year, a.Month, a.Day, a.Hour, a.Minute, 0, DateTimeKind.Utc); as I just edited – EricBDev May 21 '19 at 13:25
  • This does not strip the seconds from the result either. It only sets them to 0. – IOviSpot Dec 26 '21 at 11:14
  • This was safe direct and easy readable journey and got me there. – Honza P. Feb 11 '22 at 15:47
50

You can do

DateTime dt = DateTime.Now;
dt = dt.AddSeconds(-dt.Second);

to set the seconds to 0.

ChrisC73
  • 1,747
  • 13
  • 14
12

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);
clamchoda
  • 3,427
  • 2
  • 33
  • 69
7

A simple solution which strips both seconds and milliseconds from a DateTime:

DateTime dt = DateTime.Now;
DateTime secondsStripped = dt.Date.AddHours(dt.Hour).AddMinutes(dt.Minute);
AlliterativeAlice
  • 10,893
  • 8
  • 45
  • 68
5

How about this rather elegant solution?

new DateTime(DateTime.UtcNow.Ticks / 600000000 * 600000000)

... which will also strip any milli/micro/nanoseconds.

blackforest-tom
  • 428
  • 4
  • 8
  • There's an extra 0 there. You mean / 10_000_000. – AlexDev Mar 07 '19 at 21:00
  • The question was about stripping seconds off a DateTime. But you were right about something being wrong. There are 60 seconds in a minute, not 100 ;) I fixed the answer. By the way: you might wanna update your answer too, since its wrong (not stripping off seconds). Also the syntax error (the underscores) aren't too obvious for junior coders. – blackforest-tom Mar 19 '19 at 16:15
  • ok. since I didn't see a 6 I thought you were trying to strip the milliseconds. – AlexDev Mar 20 '19 at 12:05
2
DateTime time = DateTime.Now;
string timestring = time.ToString("g");

ToString("g") will convert DateTime to string and remove seconds.

Output: 03/29/2018 11:11 PM

Harun Diluka Heshan
  • 1,079
  • 2
  • 18
  • 28
Pirate
  • 1,103
  • 1
  • 8
  • 18
  • You'd have to parse it back to a DateTime though to be compliant with OP's request to get a DateTime, not a string. –  Sep 08 '19 at 12:28
2
// 2.3 - 0.3 = 2.0
public static DateTime Floor(this DateTime value, TimeSpan interval) {
    var mod = value.Ticks % interval.Ticks;
    return value.AddTicks( -mod );
}

// 2.3 - 0.3 + 1 = 3.0
public static DateTime Ceil(this DateTime value, TimeSpan interval) {
    var mod = value.Ticks % interval.Ticks;
    if (mod != 0) return value.AddTicks( -mod ).Add( interval );
    return value;
}
adiga
  • 31,610
  • 8
  • 53
  • 74
Denis535
  • 3,107
  • 3
  • 23
  • 33
2

And as an extension, keeps the kind and also trims milliseconds

public static DateTime TrimSeconds(this DateTime a)
{
    return new DateTime(a.Year, a.Month, a.Day, a.Hour, a.Minute, 0, a.Kind);
}
BobbyTables
  • 4,008
  • 1
  • 27
  • 35
  • This does not strip the seconds from the result either. It only sets them to 0. – IOviSpot Dec 26 '21 at 11:15
  • @wEight And what happens to the zeroes in the original questions sample of the expexted results? I dont think anyone expects them to be "gone" from a DateTime – BobbyTables Dec 27 '21 at 07:57
1

To get a local time you can use:

var dt = DateTime.Now;
dt = dt.AddTicks(-(dt.Ticks % (60 * 10_000_000)));
AlexDev
  • 3,445
  • 27
  • 35