FormatTime DateTime(?)

.FormatTime()

Formats a time as a string in the form "H:mm(:ss)" ie: 9:45:04 with optional seconds.

Method

public static string FormatTime(this DateTime dt, bool showSec = true)
public static string FormatTime(this DateTime? dt, bool showSec = true)
DateTime(?) dt

DateTime or DateTime? value to convert into the string format "H:mm(:ss)".

bool showSec
Default: true

Optionally removes the seconds from the display to show only minutes in the format "H:mm" ie: 9:45.

Example

C#
string now = DateTime.Now.FormatDateTime();