Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

595
Views
Strip seconds from datetime

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

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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);
over 4 years ago · Santiago Trujillo Report

0

You can do

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

to set the seconds to 0.

over 4 years ago · Santiago Trujillo Report

0

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);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!