Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude seconds from DateTime.ToString()

Tags:

c#

I am using DateTime.Now.ToString() in a windows service and it is giving me output like "7/23/2010 12:35:07 PM " I want to exclude the second part, displaying only up to minute.

So how to exclude seconds from that format...?

like image 580
Amit Patil Avatar asked Jul 23 '10 07:07

Amit Patil


2 Answers

Output it as short date pattern:

DateTime.Now.ToString("g") 

See MSDN for full documentation.

like image 144
Mikael Svenson Avatar answered Sep 20 '22 15:09

Mikael Svenson


You need to pass in a format string to the ToString() function:

DateTime.Now.ToString("g") 

This option is culture aware.

For this kind of output you could also use a custom format string, if you want full control:

DateTime.Now.ToString("MM/dd/yyyy hh:mm") 

This will output exactly the same regardless of culture.

like image 28
Oded Avatar answered Sep 21 '22 15:09

Oded