Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip seconds from datetime

Tags:

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

like image 594
Deependra Singh Avatar asked Jul 23 '15 04:07

Deependra Singh


2 Answers

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); 
like image 114
Mike Hixson Avatar answered Oct 02 '22 14:10

Mike Hixson


You can do

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

to set the seconds to 0.

like image 40
ChrisC73 Avatar answered Oct 02 '22 15:10

ChrisC73