A date and time format string defines the text representation of a DateTime that results from a formatting operation. It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time. A custom format string consists of one or more custom date and time format specifiers.
Usually a format string is used with DateTime.ToString() or string.Format()
Examples// DateTime to String DateTime.Today.ToString("MMM dd, yyyy") // Mar 05, 2015 string.Format("{0:dd/MM/yy HH:mm:ss}", DateTime.Now) // 05/03/2015 14:05:33 string.Format("{0:hh:mm:ss tt}" , DateTime.Now) // 02:05:33 PM // String to DateTime string dateString = "Mar 05, 2015"; DateTime dateTime = DateTime.ParseExact(dateString , "MMM dd, yyyy", null);Format Specifiers cheat sheet
Day of the month - "d", "dd" Name of the day of the week - "ddd", "dddd" Month - "M", "MM", "MMM", "MMMM" Year - "y", "yy", "yyy", "yyyy" Hour (12 hour clock) - "h", "hh" Hour (24 hour clock) - "H", "HH" AM/PM designator - "t", "tt" Minute - "m", "mm" Second - "s", "ss"
Download DateTime Formatting Utility from Microsoft : The Format Utility (Formatter.exe) is a Windows Forms application that allows you to apply standard or custom format strings to either numeric values or date and time values and to determine how they affect the result string.
No comments:
Post a Comment