Custom Date and Time Format Strings on MSDN
Link above seems to use {0:MM/dd/yy H:mm:ss zzz} a lot.
I understand all the letters and formatting options but I can't seem to find what the preceding "0:" is for?
The {0} is a composite formatting placeholder, meaning the first item in the format value list. For details, see this MSDN article, in particular, the section called "Composite Formatting" near the bottom, or the larger article specifically about Composite Formatting. But, to summarize:
In .NET there are two kinds of string formatting you can do: ToString formatting and composite formatting. Both of them use the same custom format string syntax.
When you have a single object, like a DateTime variable, and you call DateTime.ToString() on that object, you can pass a formatting string and it will apply to that object, and format it according to your pattern. But if you have more than one object and you want to build a complex string that includes their values, you instead call String.Format. That function expects a "format string" that contains placeholders where the variable bits go, which look like {0:g} or {5:MM/dd/yy} or something. The remainder of the parameters to String.Format is a list of variables. The {0} placeholder is the first variable, {5} is the 6th, etc.
Related
I found out that {:?} prints an entire array in Rust. I want to know what is it called and how exactly it works. Is it only limited to printing arrays or could it also be used elsewhere for other purposes?
This is explained (along with the rest of the formatting syntax) in the std::fmt documentation.
{...} surrounds all formatting directives. : separates the name or ordinal of the thing being formatted (which in this case is omitted, and thus means "the next thing") from the formatting options. The ? is a formatting option that triggers the use of the std::fmt::Debug implementation of the thing being formatted, as opposed to the default Display trait, or one of the other traits (like UpperHex or Octal).
Thus, {:?} formats the "next" value passed to a formatting macro, and supports anything that implements Debug.
The Debug trait is one of the most commonly used in Rust.
It allows you to format the output in a programmer-facing, debugging context. The way you typically use it is like this:
let v = vec![1, 2, 3];
let s = format!("{:?}", v);
Also, as of Rust 1.58 you can Debug-format a variable by putting it right after the opening curly bracket, like this:
let s = format!("{v:?}");
If you want to Debug-format a custom type, such as a struct, you can simply use derive like this:
#[derive(Debug)]
struct Person {
name: String,
age: u8,
}
I'm creating a custom directive that I want to use to display the value of a field and an optional suffix (expected for units and such). Note that my example is shortened to stay concise.
My template looks something like
<div class="my-value">{{boundValue}}{{boundSuffix}}</div>
For the value, I'm using a two-way binding (=) and for the suffix, I'm using a string binding (&).
It worked great when I bound ° into the suffix to display a temperature, but when I tried to bind in meters (note, there's a leading space - I don't want it pushed up against the number) the leading space seems to get trimmed and my result ends up looking like 123meters.
Using the chrome developer tools, I added a link function and inspected the directive's scope. By the time it reaches the link function, boundSuffix has already been trimmed. It seems like Angular is pulling some shenanigans on my behind the hood. Is there any way for me to avoid this trimming?
It's better to use angular filters to solve your problem. Filters allow to format your output as currency or as UPPERCASE (for example). Try to look here for more info. And here is working example
I am writing a common control that will be used to format data inside a grid. It has 2 parameters that user can set:
filter (string) that is used to format value
parameters (any[]) that are used by the filter
In the code I am going to call $filter(filter)(value, ...) - here is my problem. How do I pass my parameters? Each filter can have from no parameters to who knows how many. So, is there a nice way to pass variable number of parameters in Angular? So far I did not run into a way of doing it.
You should be able to do:
$filter(filter).apply(this, parameters)
Ext.Date contains formats a and A for am/pm or AM/PM, respectively.
I want to add a format, call it b, for a/p without the m. I have searched parseFunctions and formatFunctions but did not find where the old format is defined.
Can anyone shed some light on this matter?
Have a look at formatCodes in Ext.Date:
The base format-code to formatting-function hashmap used by the format
method. Formatting functions are strings (or functions which return
strings) which will return the appropriate value when evaluated in the
context of the Date object from which the format method is called. Add
to / override these mappings for custom date formatting.
I am trying to add a special character (specifically the ndash) to a Model field's help_text. I'm using it in the Form output so I tried what seemed intuitive for the HTML:
help_text='2 – 30 characters'
Then I tried:
help_text='2 \2013 30 characters'
Still no luck. Thoughts?
django escapes all html by default. try wrapping your string in mark_safe
You almost had it on your second try. First you need to declare the string as Unicode by prefacing it with a u. Second, you wrote the codepoint wrong. It needs a preface as well; like \u.
help_text=u'2\u201330 characters'
Now it will work and has the added benefit of not polluting the string with HTML character entities. Remember that field value could be used elsewhere, not just in the Form display output. This tip is universal for using Unicode characters in Python.
Further reading:
Unicode literals in Python, which mentions other codepoint prefaces (\x and \U)
PEP263 has simple instructions for using actual raw Unicode characters in a source file.