I know that I can get the correct time format from the user settings in the OS by using datetime.ToString(“G”), however I would like to add the leading zeros to ensure the date/time aligns in a column. Is there a way to format the time with leading zeros (with out using a custom format string) and get it to output in the format set by the user?
Share
The “G” and “g” patterns depend on the “d” (short date) pattern. See http://msdn.microsoft.com/en-us/library/8tfzyc64.aspx.
You could construct a custom
CultureInfoorDateTimeFormatInfoobject based on theCultureInfo.CurrentCulture, but instead of copying theShortDatePatterndirectly from the souce culture, replace any single-letter occurrence of “M” or “d” with “MM” and “dd”, respectively.