tags:

views:

95

answers:

7

Hi All...

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...?

+5  A: 

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.

Oded
+6  A: 

You could use a format:

DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");
Darin Dimitrov
+8  A: 

Output it as short date pattern:

DateTime.Now.ToString("g")

See MSDN for full documentation.

Mikael Svenson
This is the best solution, since it gives what the OP wants in en-US culture but is also culture-dependent in general.
Noldorin
A: 

please refer this

http://www.csharp-examples.net/string-format-datetime/

Kishore Kumar
A: 

Try DateTime.Now.ToShortDateString() + " " + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString()

mohang
This will only display the date part. You would have to add ToShortTimeString as well.
Mikael Svenson
A: 

You might want to do something like DateTime.Now.ToString("M/d/yyyy hh:mm");

for more information look at Custom Date and Time Format Strings

nyn3x
A: 

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

Encounter