I have a string which represents a DataTime value, and I want to workout what string format was used to create the string.
For example
- Given "Wednesday 27 Jan 2010" I expect "dddd dd MMM yyyy"
- Given "2010 01 27" I expect "yyyy MM dd"
Assume that the date is close to DateTime.Now and relates to the CurrentCulture. So given that we have en-GB culture
- Given "01 01 2010" I expect "dd MM yyyy"
Is there a simple way to do this?
Date and time format strings A date and time format string is a string of text used to interpret data values containing date and time information. Each format string consists of a combination of formats from an available format type. Some examples of format types are day of week, month, hour, and second.
string dateString = int. Parse("20120321"). ToString("yyyy/MM/dd");
uses a default date format to store and manipulate strings that represent dates. To specify the default date format, enter a date format in the DateTime Format String attribute in the data viewer configuration. By default, the date format is MM/DD/YYYY HH24:MI:SS.US.
A date and time format string defines the text representation of a DateTime or DateTimeOffset value 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.
All formatting can be done also using DateTime.ToString method. There are following custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone).
The Month ("M", "m") Format Specifier. The "M" or "m" standard format specifier represents a custom date and time format string that is defined by the current DateTimeFormatInfo.MonthDayPattern property. For example, the custom format string for the invariant culture is "MMMM dd".
The universal full ("U") format specifier. The "U" standard format specifier represents a custom date and time format string that is defined by a specified culture's DateTimeFormatInfo.FullDateTimePattern property. The pattern is the same as the "F" pattern. However, the DateTime value is automatically converted to UTC before it is formatted.
The simplest thing to do is look at the reference for datetime formatting strings and working them out.
You may be able to use reflection in order to get this, or use disassembler (such as ILDASM) to find all the strings used in an assembly and guess at which ones are datetime formatting string.
If disassmbling, you can search for the days of the week and for a string starting with dddd
, which should tell you how the original was constructed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With