In docs for Microsoft C# DateTime.ToString Method (String, IFormatProvider) says:
The provider parameter defines the pattern that corresponds to the standard format specifiers, as well as the symbols and names of date and time components.
I have noticed that docs only mentioned the standard format specifiers that is acting with provider parameter
Could someone explain to me why only standard format specifiers have been mentioned here "acting with provider parameter" instead of standard and Custom format specifiers?
Because custom formats are supported by a different interface, ICustomFormatter. Most concrete implementations support both.
IMHO, I strongly suspect this paragraph is try to explain that those standard format specifiers are defined in the IFormatProvider itself.
For example;
The d format specifier represents the ShortDatePattern of the format provider.
The D format specifier represents the LongDatePattern of the format provider.
The g format specifier represents the combination of ShortDatePattern and ShortTimePattern of the format provider.
The s format specifier represents the SortableDateTimePattern of the format provider which is a static value that won't change by the format provider.
and more..
As you can see, those format specifiers defined by the IFormatProvider itself. Their representing values defined what those properties are defined.
On the other hand, the custom date and time format specifiers are not defined in the IFormatProvider itself.
For example, is there any property in the IFormatProvider that defines fffffff, hh or zzz custom specifiers? No.
I think that's the "main" message on this paragraph.
Related
In the First overload of ParseExact method
public static DateTime ParseExact (string s, string format, IFormatProvider provider);
according to Microsoft:
If format is a custom format pattern that does not include date or time separators (such as "yyyyMMddHHmm"), use the invariant culture for the provider parameter and the widest form of each custom format specifier. For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H".
In particular if we use stander format pattern we could use any other cultures
what is the really purpose of using invariant culture and widest custom specifier if we use custom format pattern that does not include date or time separators if we use?
The purpose of InvariantCulture is to have a well-known way to format dates and numbers, that does not depend on the system or user locale.
You should use it every time you format something that is not meant to be parsed by humans. For example in a JSON or XML file you want to store the date in ISO format so there is no ambiguity. On the other hand if you display the date on the screen, you generally respect the user's choice of culture and display it in the preferred way.
If you use a custom format, then it would surely be good if the resulting string can be parsed back into the exact same date and time. In order to do that without separators you have to use fixed length strings for each component.
// omitting CultureInfo.InvariantCulture for brevity
var dt = new DateTime(2018,1,2,3,45,6);
dt.ToString("yyyyMMddHHmmss") // returns "20180102034506"
dt.ToString("yyyyMdHms") // returns "2018123456"
You can easily see that the second one is not unique, i.e. there are other dates which will return the same string. In order to avoid that you use the wider form of each component.
the real purpose of invariant culture is taken taken from a an answer in this question, as it describes it's purpose best:
Not all cultures use the same format for dates and decimal / currency
values.
This will matter for you when you are converting input values (read)
that are stored as strings to DateTime, float, double or decimal. It
will also matter if you try to format the aforementioned data types to
strings (write) for display or storage.
If you know what specific culture that your dates and decimal /
currency values will be in ahead of time, you can use that specific
CultureInfo property (i.e. CultureInfo("en-GB")). For example if you
expect a user input.
The CultureInfo.InvariantCulture property is used if you are
formatting or parsing a string that should be parseable by a piece of
software independent of the user's local settings.
To sum this up, the invariant culture will help with conversions be a string stored to float, decimal or asDateTime, it is here also to help when you are trying to format or parse a string that should be parseable by a piece of software independent of the user's local settings, as the quote said and that is what
widest custom specifier
means by that.
I am trying to change the default date format by the way below but the format wont change and there is no errors
I am excepting to get date in Arabic format. What I am getting now is the default system format date like this (12:00:00 03/01/2011) while I want to overwrite the system format date
Note: Working under oracle11g
TBNextDate.Text = Convert.ToDateTime(oraReder[4])
.ToString("hh:mm:ss dd/MM/yyyy", new CultureInfo("ar-AE"));
According to MSDN:
The provider parameter defines the pattern that corresponds to the standard format specifiers, as well as the symbols and names of date and time components.
When you specify a specific format like you do that does not include any name specifiers, the only thing the culture changes is the separator symbol (e.g. / or .)
If you want to use a date pattern specific to that culture, use a standard format string instead:
TBNextDate.Text = Convert.ToDateTime(oraReder[4]).ToString("d", new CultureInfo("ar-AE"));
I provide an application where the user is able to format any date by any given format string.
The problem I discovered is the following:
Let's say, I want to format the date as single month index. This is, for today "6".
But:
DateTime.Today.ToString("dd.MM.yyyy") => 26.06.2014<br/>
DateTime.Today.ToString("M") => **26 Juni**<br/>
DateTime.Today.ToString("dM") => 266
Using msdn I found out, that "M" is - if used singular - a standard format specifier, but if used with other chars, its a custom format specifier (http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx; http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx)
Question: How to force interpreting the given format string always as custom format specifier?
In your second case, you need to specify that the M is a custom format specifier by providing the % sign:
Console.WriteLine(DateTime.Today.ToString("%M"));
Prints:
6
According to MSDN: Custom Date and Time Format Strings and MSDN: Using Single Custom Format Specifiers, the % sign:
Defines the following character as a custom format specifier.
I guess if the user can specify the string, you should always escape it with %.
i am trying to format the date to this format. 01/20/2013 02:30PM EDT, using this
LastModified.ToString("MM/dd/yyyy HH:mmtt");
but the result is coming like this
01-20-2013 02:30PM dont know why it is showing '-' instead '/'.
Also, for timezome, it seems there is only format available like +02:00. But I want timezone as string, i could not find any format for this, how can I get is as string like EDT/PST/IST etc?
From the MSDN page on custom date and time format strings:
The "/" custom format specifier represents the date separator, which is used to differentiate years, months, and days. The appropriate localized date separator is retrieved from the DateTimeFormatInfoDateSeparator property of the current or specified culture.
If you want it to definitely use /, you should either use the invariant culture, or quote the slash ("MM'/'dd'/'yyyy hh':'mmtt"). Note that I've quoted the time separator as well, as that can vary by culture too. I've also changed it to use the 12 hour clock, as per Arshad's answer.
When using a custom date/time format, you should probably use the invariant culture anyway. (For example, it seems odd to use a UK culture to format the string in a US-centric way - today would normally be represented as 02/05/2013 in the UK, not 05/02/2013.)
In terms of a time zone specifier - I don't know any way to use the time zone abbrevation within date/time formatting. I would personally advise against using abbreviations anyway, as they can be ambiguous and confusing. I can't see anything within TimeZoneInfo which even exposes that information for you to manually add it.
(It's possible that in Noda Time we'll support formatting with the abbreviation, but probably not parsing, precisely because of the ambiguity.)
i have found one mistake is that ,HH means time in 24 HRS format. You can try
string date = "01/20/2013 02:30PM";
DateTime dtTime;
if (DateTime.TryParseExact(date, "MM/dd/yyyy hh:mmtt",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out dtTime))
{
Console.WriteLine(dtTime);
}
Formatting of DateTime is influenced by your Culture and your format string. Your current culture (Thread.CurrentThread.CurrentCulture) uses the - as the default seperator of your date components.
The Culture of India uses - and since you are from Inda, it would make sence (see: http://en.wikipedia.org/wiki/Date_and_time_notation_in_India)
Two options:
Choose a correct Culture which uses the / by default as seperator. For example: LastModified.ToString("MM/dd/yyyy HH:mmtt", new CultureInfo("en-US"));
Or reformat your format string with the escape character \. For example: For example: LastModified.ToString("MM\/dd\/yyyy HH:mmtt");
See also: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
how can i create time in this format (DATE_ISO8601) in C# ?
2009-05-18T16:34:09.423-0700
thank you!
You can use the O or o format specifier.
DateTime.Now.ToString("O");
From MSDN (Standard Date and Time Format Strings):
The pattern for this specifier reflects a defined standard (ISO 8601). Therefore, it is always the same regardless of the culture used or the format provider supplied. Strings that are passed to the Parse or ParseExact method must conform exactly to this custom format pattern, or a FormatException is thrown.