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

231
Views
Compare two DateTimes with ignoring seconds and milliseconds

I take two object from database. One is a filename with date init and second one is a DateTime object like 2021-08-08 17:32:07.880.

First, I converted filename to datetime with the code shown here:

var fileDate = DateTime.ParseExact(filename, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);

I have to check that the difference between the first date and the second date is 3 hours 15 min or simply 3 hours.

How do I delete seconds and milliseconds of date 2, and compare them?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I'd go similar to MatJ's recommendation:

You've got your file time, and your DB time, which might have seconds and milliseconds on it. If you do the later one minus the earlier one you get a timespan representing the length of time between the datetimes

dBDate - fileDate

Timespans have a TotalMinutes property that is a decimal. A timespan of 5 minutes 45 seconds would have a TotalMinutes of 5.75

So, if we cast that to an int it cuts off the seconds; simples!

var t = (int)((dBDate - fileDate).TotalMinutes);

Now you can compare your t for equality to 180 (3h) or 195 (3h15h

over 4 years ago · Santiago Trujillo Report

0

It is very easy to do !

Try following code :

TimeSpan timeSpan = (firstDate - secondDate)
timeSpan.CompareTo(new TimeSpan(3, 15, 0)) // hrs, mins, seconds

This CompareTo method will return 1 if difference between two times is greater than 3 hrs and 15 mins, otherwise, it will return -1

PS: firstDate and secondDate are in DateTime

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!