Strings.Format(Object, String) Metoda

Definice

Vrátí řetězec formátovaný podle pokynů obsažených ve String výrazu formátu.Returns a string formatted according to instructions contained in a format String expression.

public static string Format (object? Expression, string? Style = "");
public static string Format (object Expression, string Style = "");
static member Format : obj * string -> string
Public Function Format (Expression As Object, Optional Style As String = "") As String

Parametry

Expression
Object

Povinná hodnota.Required. Libovolný platný výrazAny valid expression.

Style
String

Nepovinný parametr.Optional. Platný pojmenovaný nebo uživatelsky definovaný výraz formátu String .A valid named or user-defined format String expression.

Návraty

String

Řetězec formátovaný podle pokynů obsažených ve String výrazu formátu.A string formatted according to instructions contained in a format String expression.

Příklady

Tento příklad ukazuje různá použití Format funkce pro formátování hodnot pomocí String formátů i uživatelsky definovaných formátů.This example shows various uses of the Format function to format values using both String formats and user-defined formats. V případě oddělovače data ( / ), oddělovače času ( : ) a indikátory AM/PM ( t a tt ) je skutečný formátovaný výstup zobrazený systémem závislý na nastavení národního prostředí, které kód používá.For the date separator (/), time separator (:), and the AM/PM indicators (t and tt), the actual formatted output displayed by your system depends on the locale settings the code is using. Když jsou časy a kalendářní data zobrazeny ve vývojovém prostředí, je použit krátký formát času a formát krátkého data národního prostředí kódu.When times and dates are displayed in the development environment, the short time format and short date format of the code locale are used.

Poznámka

Pro národní prostředí, která používají 24hodinové hodiny, se indikátory AM/PM ( t a tt ) nezobrazí nic.For locales that use a 24-hour clock, the AM/PM indicators (t and tt) display nothing.

Dim testDateTime As Date = #1/27/2001 5:04:23 PM#
Dim testStr As String
' Returns current system time in the system-defined long time format.
testStr = Format(Now(), "Long Time")
' Returns current system date in the system-defined long date format.
testStr = Format(Now(), "Long Date")
' Also returns current system date in the system-defined long date 
' format, using the single letter code for the format.
testStr = Format(Now(), "D")

' Returns the value of testDateTime in user-defined date/time formats.
' Returns "5:4:23".
testStr = Format(testDateTime, "h:m:s")
' Returns "05:04:23 PM".
testStr = Format(testDateTime, "hh:mm:ss tt")
' Returns "Saturday, Jan 27 2001".
testStr = Format(testDateTime, "dddd, MMM d yyyy")
' Returns "17:04:23".
testStr = Format(testDateTime, "HH:mm:ss")
' Returns "23".
testStr = Format(23)

' User-defined numeric formats.
' Returns "5,459.40".
testStr = Format(5459.4, "##,##0.00")
' Returns "334.90".
testStr = Format(334.9, "###0.00")
' Returns "500.00%".
testStr = Format(5, "0.00%")

Poznámky

String.FormatMetoda také nabízí podobné funkce.The String.Format method also provides similar functionality.

Pokud formátujete nemístní číselné řetězce, měli byste použít uživatelsky definovaný číselný formát, který zajistí, že budete mít požadovaný vzhled.If you are formatting a nonlocalized numeric string, you should use a user-defined numeric format to ensure that you get the look you want.

Pokud se pokusíte formátovat číslo bez zadání Style , Format funkce poskytne funkci podobnou Str funkci, i když má mezinárodní povědomí.If you try to format a number without specifying Style, the Format function provides functionality similar to the Str function, although it is internationally aware. Kladné hodnoty formátované jako řetězce používající Format funkci ale neobsahují počáteční místo vyhrazené pro znaménko hodnoty. ty, které se převedly pomocí Str funkce, si zachovají počáteční prostor.However, positive numbers formatted as strings using the Format function don't include a leading space reserved for the sign of the value; those converted using the Str function retain the leading space.

Různé formáty pro různé číselné hodnotyDifferent Formats for Different Numeric Values

Uživatelsky definovaný výraz formátu pro čísla může mít jednu až tři části, které jsou oddělené středníky.A user-defined format expression for numbers can have from one to three sections separated by semicolons. Pokud Style argument Format funkce obsahuje jeden z předdefinovaných číselných formátů, je povolen pouze jeden oddíl.If the Style argument of the Format function contains one of the predefined numeric formats, only one section is allowed.

Pokud použijeteIf you use Toto je výsledekThis is the result
Pouze jeden oddílOne section only Výraz formátu se používá na všechny hodnoty.The format expression applies to all values.
Dva oddílyTwo sections První oddíl se používá na kladné hodnoty a nuly, druhý platí pro záporné hodnoty.The first section applies to positive values and zeros; the second applies to negative values.
Tři oddílyThree sections První oddíl platí pro kladné hodnoty, druhý se vztahuje na záporné hodnoty a třetí platí pro nuly.The first section applies to positive values, the second applies to negative values, and the third applies to zeros.

Následující příklad obsahuje dva oddíly: první definuje formát pro kladné hodnoty a nuly; Druhá část definuje formát pro záporné hodnoty.The following example has two sections: the first defines the format for positive values and zeros; the second section defines the format for negative values. Vzhledem k Style Format tomu, že argument funkce přebírá řetězec, je uzavřen v uvozovkách.Since the Style argument of the Format function takes a string, it is enclosed by quotation marks.

Dim style1 As String = "$#,##0;($#,##0)"

Pokud zahrnete středníky, mezi kterými nic není, chybějící oddíl se vytiskne pomocí formátu kladné hodnoty.If you include semicolons with nothing between them, the missing section is printed using the format of the positive value. Například následující formát zobrazuje kladné a záporné hodnoty pomocí formátu v první části a zobrazuje, Zero zda je hodnota nulová.For example, the following format displays positive and negative values using the format in the first section and displays Zero if the value is zero.

Dim style2 As String = "$#,##0;;\Z\e\r\o"

Předdefinované číselné formátyPredefined Numeric Formats

Následující tabulka uvádí názvy předdefinovaných číselných formátů.The following table identifies the predefined numeric format names. Ty mohou být použity názvem jako Style argument pro Format funkci:These may be used by name as the Style argument for the Format function:

Název formátuFormat name DescriptionDescription
General Number, G nebo gGeneral Number, G, or g Zobrazí číslo bez oddělovače tisíců.Displays number with no thousand separator.

Například Format(&H3FA, "g") vrátí 1018 .For example, Format(&H3FA, "g") returns 1018.
Currency, C nebo cCurrency, C, or c V případě potřeby zobrazí číslo s oddělovačem tisíců. zobrazí dvě číslice napravo od oddělovače desetinných míst.Displays number with thousand separator, if appropriate; displays two digits to the right of the decimal separator. Zobrazení závisí na nastavení národního prostředí systému.Output is based on system locale settings.

Například Format(1234567, "c") vrátí $1,234,567.00 .For example, Format(1234567, "c") returns $1,234,567.00.
Fixed, F nebo fFixed, F, or f Zobrazí alespoň jednu číslici vlevo a dvě číslice vpravo od oddělovače desetinných míst.Displays at least one digit to the left and two digits to the right of the decimal separator.

Například Format(1234567, "f") vrátí 1234567.00 .For example, Format(1234567, "f") returns 1234567.00.
Standard, N nebo nStandard, N, or n Zobrazí číslo s oddělovačem tisíců, alespoň jednu číslici nalevo a dvě číslice napravo od oddělovače desetinných míst.Displays number with thousand separator, at least one digit to the left and two digits to the right of the decimal separator.

Například Format(1234567, "n") vrátí 1,234,567.00 .For example, Format(1234567, "n") returns 1,234,567.00.
Percent Zobrazí číslo vynásobené 100 se znakem procenta (%) připojeným bezprostředně zprava; vždy zobrazí dvě číslice vpravo od oddělovače desetinných míst.Displays number multiplied by 100 with a percent sign (%) appended immediately to the right; always displays two digits to the right of the decimal separator.

Například Format(0.4744, "Percent") vrátí 47.44% .For example, Format(0.4744, "Percent") returns 47.44%.
P nebo pP, or p Zobrazí číslo s oddělovačem sekundy vynásobeným symbolem 100 a znakem procenta (%). připojeno k pravému a oddělenému jedním mezerou; vždy zobrazí dvě číslice napravo od oddělovače desetinných míst.Displays number with thousandths separator multiplied by 100 with a percent sign (%) appended to the right and separated by a single space; always displays two digits to the right of the decimal separator.

Například Format(0.80345, "p") vrátí 80.35 % .For example, Format(0.80345, "p") returns 80.35 %.
Scientific Používá standardní vědecký zápis, který poskytuje dvě významné číslice.Uses standard scientific notation, providing two significant digits.

Například Format(1234567, "Scientific") vrátí 1.23E+06 .For example, Format(1234567, "Scientific") returns 1.23E+06.
E nebo eE, or e Používá standardní vědecký zápis, který poskytuje šest platných číslic.Uses standard scientific notation, providing six significant digits.

Například Format(1234567, "e") vrátí 1.234567e+006 .For example, Format(1234567, "e") returns 1.234567e+006.
D nebo dD, or d Zobrazí číslo jako řetězec, který obsahuje hodnotu čísla v desítkovém formátu (desítkový formát 10).Displays number as a string that contains the value of the number in Decimal (base 10) format. Tato možnost je podporována pouze pro integrální typy ( Byte , Short , Integer , Long ).This option is supported for integral types (Byte, Short, Integer, Long) only.

Například Format(&H7F, "d") vrátí 127 .For example, Format(&H7F, "d") returns 127.
X nebo xX, or x Zobrazí číslo jako řetězec, který obsahuje hodnotu čísla v šestnáctkovém formátu (základní 16).Displays number as a string that contains the value of the number in Hexadecimal (base 16) format. Tato možnost je podporována pouze pro integrální typy ( Byte , Short , Integer , Long ).This option is supported for integral types (Byte, Short, Integer, Long) only.

Například Format(127, "x") vrátí 7f .For example, Format(127, "x") returns 7f.
Yes/No Zobrazí, No Pokud je číslo 0. v opačném případě zobrazí Yes .Displays No if number is 0; otherwise, displays Yes.

Například Format(0, "Yes/No") vrátí No .For example, Format(0, "Yes/No") returns No.
True/False Zobrazí, False Pokud je číslo 0. v opačném případě zobrazí True .Displays False if number is 0; otherwise, displays True.

Například Format(1, "True/False") vrátí True .For example, Format(1, "True/False") returns True.
On/Off Zobrazí, Off Pokud je číslo 0. v opačném případě zobrazí On .Displays Off if number is 0; otherwise, displays On.

Například Format(1, "On/Off") vrátí On .For example, Format(1, "On/Off") returns On.

Poznámky pro vývojáře inteligentního zařízeníSmart Device Developer Notes

Yes/NoFormáty, True/False a On/Off nejsou podporovány.The Yes/No, True/False, and On/Off formats are not supported.

Číselné formáty definované uživatelemUser-Defined Numeric Formats

Následující tabulka uvádí znaky, které můžete použít k vytvoření uživatelsky definovaných formátů čísel.The following table identifies characters you can use to create user-defined number formats. Tyto funkce lze použít k sestavení Style argumentu pro Format funkci:These may be used to build the Style argument for the Format function:

|ZnakCharacter|DescriptionDescription|
|-|-|
|ŽádnáNone|Zobrazí se číslo bez formátování.Displays the number with no formatting.|
|(0)(0)|Zástupný symbol číslice.Digit placeholder. Zobrazí se číslice nebo nula.Displays a digit or a zero. Pokud má výraz číslici na pozici, kde se zobrazí nula ve formátovacím řetězci, zobrazte ho; v opačném případě zobrazí na této pozici nulu.If the expression has a digit in the position where the zero appears in the format string, display it; otherwise, displays a zero in that position.

Pokud má číslo méně číslic, než je ve výrazu formátu nul (na kterékoli straně oddělovače desetinných míst), zobrazí se úvodní a/nebo koncové nuly.If the number has fewer digits than there are zeros (on either side of the decimal) in the format expression, displays leading or trailing zeros. Pokud má číslo vpravo od oddělovače desetinných míst více číslic, než je nul vpravo od oddělovače desetinných míst ve výrazu formátu, číslo se zaokrouhlí na tolik desetinných míst, kolik je nul.If the number has more digits to the right of the decimal separator than there are zeros to the right of the decimal separator in the format expression, rounds the number to as many decimal places as there are zeros. Pokud má číslo vlevo od oddělovače desetinných míst více číslic, než je nul vlevo od oddělovače desetinných míst ve výrazu formátu, zobrazí se číslice navíc beze změny.If the number has more digits to the left of the decimal separator than there are zeros to the left of the decimal separator in the format expression, displays the extra digits without modification.|
|(#)(#)|Zástupný symbol číslice.Digit placeholder. Zobrazí číslici nebo nezobrazí nic.Displays a digit or nothing. Pokud má výraz číslici na pozici, kde se # znak zobrazí ve formátu řetězec, zobrazí se. v opačném případě zobrazí na této pozici nic.If the expression has a digit in the position where the # character appears in the format string, displays it; otherwise, displays nothing in that position.

Tento symbol funguje podobně jako 0 zástupný symbol číslice, s tím rozdílem, že úvodní a koncové nuly nejsou zobrazeny, pokud má číslo méně číslic, než jsou # znaky na obou stranách oddělovač desetinných míst ve výrazu formátu.This symbol works like the 0 digit placeholder, except that leading and trailing zeros aren't displayed if the number has fewer digits than there are # characters on either side of the decimal separator in the format expression.|
|(.)(.)|Zástupný symbol oddělovače desetinných míst.Decimal placeholder. Zástupný symbol oddělovače desetinných míst určuje, kolik číslic se zobrazí vlevo a vpravo od oddělovače desetinných míst.The decimal placeholder determines how many digits are displayed to the left and right of the decimal separator. Pokud výraz formátu obsahuje pouze # znaky nalevo od tohoto symbolu; čísla menší než 1 začínají oddělovačem desetinných míst.If the format expression contains only # characters to the left of this symbol; numbers smaller than 1 begin with a decimal separator. Když budete chtít, aby se čísla menší než 1 zobrazila s úvodní nulou, potom jako první zástupný symbol číslice vlevo od oddělovače desetinných míst použijte nulu.To display a leading zero displayed with fractional numbers, use zero as the first digit placeholder to the left of the decimal separator. V některých národních prostředích se jako oddělovač desetinných míst používá čárka.In some locales, a comma is used as the decimal separator. Skutečný znak, který se ve formátovaném výstupu použije jako oddělovač desetinných míst, závisí na formátu čísel nastaveném v systému.The actual character used as a decimal placeholder in the formatted output depends on the number format recognized by your system. Proto byste měli použít jako zástupný symbol oddělovače desetinných míst ve formátech tečku i v případě, že jste v národním prostředí, které jako zástupný symbol oddělovače desetinných míst používá čárku.Thus, you should use the period as the decimal placeholder in your formats even if you are in a locale that uses a comma as a decimal placeholder. Formátovaný řetězec se zobrazí ve správném formátu národního prostředí.The formatted string will appear in the format correct for the locale.|
|(%)(%)|Zástupný symbol procentPercent placeholder. Vynásobí výraz hodnotou 100.Multiplies the expression by 100. Znak procenta ( % ) se vloží na pozici, na které se nachází v řetězci formátu.The percent character (%) is inserted in the position where it appears in the format string.|
|(,)(,)|Oddělovač tisíců.Thousand separator. Oddělovač tisíců odděluje tisíce od stovek v čísle, které má vlevo od oddělovače desetinných míst čtyři nebo více míst.The thousand separator separates thousands from hundreds within a number that has four or more places to the left of the decimal separator. Standardní použití oddělovače tisíců je určeno, pokud formát obsahuje oddělovač tisíců ohraničený zástupnými symboly číslic ( 0 nebo # ).Standard use of the thousand separator is specified if the format contains a thousand separator surrounded by digit placeholders (0 or #).

Oddělovač tisíců bezprostředně vlevo od oddělovače desetinných míst (ať už je oddělovač desetinných míst uvedený nebo ne) nebo znak úplně vpravo v řetězci znamená to, že číslo se má vydělit 1000 a podle potřeby zaokrouhlit.A thousand separator immediately to the left of the decimal separator (whether or not a decimal is specified) or as the rightmost character in the string means "scale the number by dividing it by 1,000, rounding as needed." Čísla menší než 1 000, ale větší nebo rovna 500 jsou zobrazena jako 1 a čísla menší než 500 se zobrazí jako 0 .Numbers smaller than 1,000 but greater or equal to 500 are displayed as 1, and numbers smaller than 500 are displayed as 0. Dva sousední oddělovače tisíců v této pozici mění velikost pomocí faktoru 1 milion a dalšího faktoru 1000 pro každý další oddělovač.Two adjacent thousand separators in this position scale by a factor of 1 million, and an additional factor of 1,000 for each additional separator.

Několik oddělovačů tisíců na libovolné jiné pozici než bezprostředně vlevo od oddělovače desetinných míst nebo pozice úplně vpravo v řetězci se jednoduše považuje za požadované použití oddělovače tisíců.Multiple separators in any position other than immediately to the left of the decimal separator or the rightmost position in the string are treated simply as specifying the use of a thousand separator. V některých národních prostředích se jako oddělovač tisíců používá tečka.In some locales, a period is used as a thousand separator. Skutečný znak, který se ve formátovaném výstupu použije jako oddělovač tisíců, závisí na formátu čísel nastaveném v systému.The actual character used as the thousand separator in the formatted output depends on the Number Format recognized by your system. Proto byste měli použít jako oddělovač tisíců ve formátech čárku i v případě, že jste v národním prostředí, které jako oddělovač tisíců používá tečku.Thus, you should use the comma as the thousand separator in your formats even if you are in a locale that uses a period as a thousand separator. Formátovaný řetězec se zobrazí ve správném formátu národního prostředí.The formatted string will appear in the format correct for the locale.

Zvažte například tyto tři následující řetězce formátu:For example, consider the three following format strings:

- "#,0.", které pomocí oddělovače tisíců formátuje číslo 100 000 000 jako řetězec "100 000 000".- "#,0.", which uses the thousands separator to format the number 100 million as the string "100,000,000".
- "#0,.", který používá škálování podle faktoru 1000 k formátování čísla 100 000 000 jako řetězce "100000".- "#0,.", which uses scaling by a factor of one thousand to format the number 100 million as the string "100000".
- "#,0,.", který používá oddělovač tisíců a škálování podle 1000 k formátování čísla 100 000 000 jako řetězce "100 000".- "#,0,.", which uses the thousands separator and scaling by one thousand to format the number 100 million as the string "100,000".|
|(:)(:)|Oddělovač času.Time separator. V některých národních prostředích se k reprezentaci oddělovače času můžou používat jiné znaky.In some locales, other characters may be used to represent the time separator. Oddělovač času při formátování hodnot času odděluje hodiny, minuty a sekundy.The time separator separates hours, minutes, and seconds when time values are formatted. Skutečný znak, který se ve formátovaném výstupu použije jako oddělovač času, závisí na nastavení systému.The actual character used as the time separator in formatted output is determined by your system settings.|
|(/)(/)|Oddělovač data.Date separator. V některých národních prostředích se k reprezentaci oddělovače data můžou používat jiné znaky.In some locales, other characters may be used to represent the date separator. Oddělovač data při formátování hodnot data odděluje den, měsíc a rok.The date separator separates the day, month, and year when date values are formatted. Skutečný znak, který se ve formátovaném výstupu použije jako oddělovač data, závisí na nastavení systému.The actual character used as the date separator in formatted output is determined by your system settings.|
|(E-``E+``e-``e+)(E-``E+``e-``e+)|Vědecký formát.Scientific format. Pokud výraz formátu obsahuje alespoň jeden zástupný symbol číslice ( 0 nebo # ) nalevo od E- , E+ , e- nebo e+ , zobrazí se číslo v matematickém formátu a E nebo e je vloženo mezi číslem a jeho exponentem.If the format expression contains at least one digit placeholder (0 or #) to the left of E-, E+, e-, or e+, the number is displayed in scientific format and E or e is inserted between the number and its exponent. Počet zástupných symbolů číslic vlevo určuje počet číslic v exponentu.The number of digit placeholders to the left determines the number of digits in the exponent. Použijte E- nebo e- k umístění znaménka mínus u záporných exponentů.Use E- or e- to place a minus sign next to negative exponents. Použijte E+ nebo e+ k umístění znaménka mínus vedle záporných exponentů a znaménka plus vedle kladného exponentu.Use E+ or e+ to place a minus sign next to negative exponents and a plus sign next to positive exponents. Pokud chcete získat správné formátování, musíte zahrnout také zástupné symboly číslic napravo od tohoto symbolu.You must also include digit placeholders to the right of this symbol to get correct formatting.|
|- + $ ( )- + $ ( )|Znaky literáluLiteral characters. Tyto znaky se zobrazují přesně tak, jak jsou zadány v řetězci formátu.These characters are displayed exactly as typed in the format string. Pokud chcete zobrazit jiný znak než některý z těch, které jsou tady uvedené, dejte před něj zpětné lomítko (\) nebo ho uzavřete do uvozovek (" ").To display a character other than one of those listed, precede it with a backslash (\) or enclose it in double quotation marks (" ").|
|(\)(\)|Zobrazí další znak řetězce formátu.Displays the next character in the format string. Pokud chcete zobrazit znak, který má jako literální znak zvláštní význam, dejte před něj zpětné lomítko (\).To display a character that has special meaning as a literal character, precede it with a backslash (\). Samotné zpětné lomítko se nezobrazí.The backslash itself isn't displayed. Použití zpětného lomítka funguje stejně, jako když následující znak uzavřete do uvozovek.Using a backslash is the same as enclosing the next character in double quotation marks. Pokud chcete zobrazit zpětné lomítko, použijte v řetězci formátu dvě zpětná lomítka (`\\{2}{3}).</span><span class="sxs-lookup"><span data-stu-id="58deb-411">To display a backslash, use two backslashes (\).</span></span><br /><br /> <span data-ttu-id="58deb-412">Příklady znaků, které nelze zobrazit jako literální znaky, jsou znaky formátování data a času ( a,c,d,h,m,n,p,q,s,t,w,y,/a:), znaky číselného formátování (#,,,,0 % E ečárka a tečka) a znaky formátování řetězce (@,&,<, a> ! ).</span><span class="sxs-lookup"><span data-stu-id="58deb-412">Examples of characters that can't be displayed as literal characters are the date-formatting and time-formatting characters (a, c, d, h, m, n, p, q, s, t, w, y, /, and :), the numeric-formatting characters (#, 0, %, E, e, comma, and period), and the string-formatting characters (@, &, <, >, and !).</span></span>| |<span data-ttu-id="58deb-413">("ABC")</span><span class="sxs-lookup"><span data-stu-id="58deb-413">("ABC")</span></span>|<span data-ttu-id="58deb-414">Zobrazí řetězec, který uzavřete do uvozovek (" ").</span><span class="sxs-lookup"><span data-stu-id="58deb-414">Displays the string inside the double quotation marks (" ").</span></span> <span data-ttu-id="58deb-415">Chcete-li zahrnout řetězec v argumentu Style v rámci kódu, je nutné použít Chr(34)k uzavření textu (34je kód znaku pro uvozovky (")).</span><span class="sxs-lookup"><span data-stu-id="58deb-415">To include a string in the style argument from within code, you must useChr(34) to enclose the text (34` is the character code for a quotation mark (")).|

Příklad kódu starší verzeLegacy Code Example

Následující tabulka obsahuje příklady formátovacích výrazů pro čísla.The following table contains some sample format expressions for numbers. (V těchto příkladech se předpokládá, že nastavení národního prostředí vašeho systému je anglické – U. S.) První sloupec obsahuje formátovací řetězce pro Style argument Format funkce. ostatní sloupce obsahují výsledný výstup, pokud mají formátovaná data hodnotu zadanou v záhlaví sloupců.(These examples all assume that your system's locale setting is English-U.S.) The first column contains the format strings for the Style argument of the Format function; the other columns contain the resulting output if the formatted data has the value given in the column headings.

Formát ( Style )Format (Style) "5" formátováno jako"5" formatted as "–5" formátováno jako"-5" formatted as "0,5" formátováno jako"0.5" formatted as
Zero-length string ("") 5 -5 0.5
0 5 -5 1
0.00 5.00 -5.00 0.50
#,##0 5 -5 1
$#,##0;($#,##0) $5 ($5) $1
$#,##0.00;($#,##0.00) $5.00 ($5.00) $0.50
0% 500% -500% 50%
0.00% 500.00% -500.00% 50.00%
0.00E+00 5.00E+00 -5.00E+00 5.00E-01
0.00E-00 5.00E00 -5.00E00 5.00E-01

Předdefinované formáty data a časuPredefined Date/Time Formats

Následující tabulka uvádí předdefinované názvy formátů data a času.The following table identifies the predefined date and time format names. Ty mohou být použity názvem jako argument Style pro Format funkci:These may be used by name as the style argument for the Format function:

Název formátuFormat Name DescriptionDescription
General Date nebo GGeneral Date, or G Zobrazí datum a/nebo čas.Displays a date and/or time. Například, 3/12/2008 11:07:31 AM.For example, 3/12/2008 11:07:31 AM. Zobrazení data je určeno aktuální hodnotou jazykové verze vaší aplikace.Date display is determined by your application's current culture value.
Long Date, Medium Date nebo DLong Date, Medium Date, or D Zobrazí datum podle formátu dlouhého data aktuální jazykové verze.Displays a date according to your current culture's long date format. Například, Wednesday, March 12, 2008.For example, Wednesday, March 12, 2008.
Short Date nebo dShort Date, or d Zobrazí datum podle formátu krátkého data aktuální jazykové verze.Displays a date using your current culture's short date format. Například, 3/12/2008.For example, 3/12/2008.

dZnak zobrazuje den v uživatelsky definovaném formátu data.The d character displays the day in a user-defined date format.
Long Time, Medium Time nebo TLong Time, Medium Time, or T Zobrazuje čas pomocí formátu dlouhého času aktuální jazykové verze. Obvykle zahrnuje hodiny, minuty a sekundy.Displays a time using your current culture's long time format; typically includes hours, minutes, seconds. Například, 11:07:31 AM.For example, 11:07:31 AM.
Short Time nebo tShort Time or t Zobrazuje čas s použitím krátkého formátu času aktuální jazykové verze.Displays a time using your current culture's short time format. Například, 11:07 AM.For example, 11:07 AM.

tZnak zobrazuje AM nebo PM hodnot pro národní prostředí, která používají 12hodinový čas v uživatelsky definovaném formátu času.The t character displays AM or PM values for locales that use a 12-hour clock in a user-defined time format.
f Zobrazuje dlouhé datum a čas v závislosti na formátu aktuální jazykové verze.Displays the long date and short time according to your current culture's format. Například, Wednesday, March 12, 2008 11:07 AM.For example, Wednesday, March 12, 2008 11:07 AM.
F Zobrazuje dlouhé datum a čas v závislosti na formátu aktuální jazykové verze.Displays the long date and long time according to your current culture's format. Například, Wednesday, March 12, 2008 11:07:31 AM.For example, Wednesday, March 12, 2008 11:07:31 AM.
g Zobrazuje krátký formát data a krátkého času podle formátu aktuální jazykové verze.Displays the short date and short time according to your current culture's format. Například, 3/12/2008 11:07 AM.For example, 3/12/2008 11:07 AM.
M, mM, m Zobrazí měsíc a den v datu.Displays the month and the day of a date. Například, March 12.For example, March 12.

MZnak zobrazuje měsíc v uživatelsky definovaném formátu data.The M character displays the month in a user-defined date format. mZnak zobrazuje minuty v uživatelsky definovaném formátu času.The m character displays the minutes in a user-defined time format.
R, rR, r Formátuje datum podle RFC1123Pattern Vlastnosti.Formats the date according to the RFC1123Pattern property. Například, Wed, 12 Mar 2008 11:07:31 GMT.For example, Wed, 12 Mar 2008 11:07:31 GMT. Formátování data neupravuje hodnotu data a času.The formatted date does not adjust the value of the date and time. Před voláním funkce je nutné upravit hodnotu data a času na GMT Format .You must adjust the Date/Time value to GMT before calling the Format function.
s Formátuje datum a čas jako shodný index.Formats the date and time as a sortable index. Například, 2008-03-12T11:07:31.For example, 2008-03-12T11:07:31.

sZnak zobrazuje sekundy v uživatelsky definovaném formátu času.The s character displays the seconds in a user-defined time format.
u Formátuje datum a čas jako neplatný index GMT.Formats the date and time as a GMT sortable index. Například, 2008-03-12 11:07:31Z.For example, 2008-03-12 11:07:31Z.
U Formátuje datum a čas dlouhým datem a časem v čase GMT.Formats the date and time with the long date and long time as GMT. Například, Wednesday, March 12, 2008 6:07:31 PM.For example, Wednesday, March 12, 2008 6:07:31 PM.
Y, yY, y Formátuje datum jako rok a měsíc.Formats the date as the year and month. Například, March, 2008.For example, March, 2008.

YZnaky a y zobrazují rok v uživatelsky definovaném formátu data.The Y and y characters display the year in a user-defined date format.

Další informace o aktuálních informacích o jazykové verzi aplikace naleznete v tématu jak jazyková verze ovlivňuje řetězce v Visual Basic.For more information about the application's current culture information, see How Culture Affects Strings in Visual Basic.

Formáty data a času definované uživatelemUser-Defined Date/Time Formats

Následující tabulka obsahuje znaky, které můžete použít k vytvoření uživatelsky definovaných formátů data a času.The following table shows characters you can use to create user-defined date/time formats. Na rozdíl od dřívějších verzí Visual Basic tyto formátovací znaky rozlišují malá a velká písmena.Unlike in earlier versions of Visual Basic, these format characters are case-sensitive.

ZnakCharacter DescriptionDescription
(:)(:) Oddělovač času.Time separator. V některých národních prostředích se k reprezentaci oddělovače času můžou používat jiné znaky.In some locales, other characters may be used to represent the time separator. Oddělovač času při formátování hodnot času odděluje hodiny, minuty a sekundy.The time separator separates hours, minutes, and seconds when time values are formatted. Skutečný znak, který se ve formátovaném výstupu použije jako oddělovač času, je určen aktuální hodnotou jazykové verze vaší aplikace.The actual character that is used as the time separator in formatted output is determined by your application's current culture value.
(/)(/) Oddělovač data.Date separator. V některých národních prostředích se k reprezentaci oddělovače data můžou používat jiné znaky.In some locales, other characters may be used to represent the date separator. Oddělovač data při formátování hodnot data odděluje den, měsíc a rok.The date separator separates the day, month, and year when date values are formatted. Skutečný znak, který se ve formátovaném výstupu použije jako oddělovač data, je určen aktuální jazykovou verzí vaší aplikace.The actual character that is used as the date separator in formatted output is determined by your application's current culture.
(%)(%) Udává, že následující znak se má číst jako jednopísmenný formát bez ohledu na jakákoli koncová písmena.Used to indicate that the following character should be read as a single-letter format without regard to any trailing letters. Zároveň udává, že jednopísmenný formát se čte jako uživatelem definovaný formát.Also used to indicate that a single-letter format is read as a user-defined format. Další podrobnosti najdete v následujícím textu.See what follows for additional details.
d Zobrazí den jako číslo bez úvodní nuly (například 1 ).Displays the day as a number without a leading zero (for example, 1). Použijte %d v případě, že se jedná o jediný znak v uživatelsky definovaném číselném formátu.Use %d if this is the only character in your user-defined numeric format.
dd Zobrazí den jako číslo s úvodní nulou (například 01 ).Displays the day as a number with a leading zero (for example, 01).
ddd Zobrazí den jako zkratku (například Sun ).Displays the day as an abbreviation (for example, Sun).
dddd Zobrazí den jako úplný název (například Sunday ).Displays the day as a full name (for example, Sunday).
M Zobrazí měsíc jako číslo bez úvodní nuly (například leden je reprezentován jako 1 ).Displays the month as a number without a leading zero (for example, January is represented as 1). Použijte %M v případě, že se jedná o jediný znak v uživatelsky definovaném číselném formátu.Use %M if this is the only character in your user-defined numeric format.
MM Zobrazí měsíc jako číslo s úvodní nulou (například 01/12/01 ).Displays the month as a number with a leading zero (for example, 01/12/01).
MMM Zobrazí měsíc jako zkratku (například Jan ).Displays the month as an abbreviation (for example, Jan).
MMMM Zobrazí měsíc jako úplný název měsíce (například January ).Displays the month as a full month name (for example, January).
gg Zobrazí řetězec období/období (například A.D. ).Displays the period/era string (for example, A.D.).
h Zobrazuje hodinu jako číslo bez počátečních nul pomocí 12 hodin (například 1:15:15 PM ).Displays the hour as a number without leading zeros using the 12-hour clock (for example, 1:15:15 PM). Použijte %h v případě, že se jedná o jediný znak v uživatelsky definovaném číselném formátu.Use %h if this is the only character in your user-defined numeric format.
hh Zobrazuje hodinu jako číslo s počátečními nulami pomocí 12 hodin (například 01:15:15 PM ).Displays the hour as a number with leading zeros using the 12-hour clock (for example, 01:15:15 PM).
H Zobrazuje hodinu jako číslo bez počátečních nul pomocí 24 hodin (například 1:15:15 ).Displays the hour as a number without leading zeros using the 24-hour clock (for example, 1:15:15). Použijte %H v případě, že se jedná o jediný znak v uživatelsky definovaném číselném formátu.Use %H if this is the only character in your user-defined numeric format.
HH Zobrazuje hodinu jako číslo s počátečními nulami, a to pomocí 24 hodin (například 01:15:15 ).Displays the hour as a number with leading zeros using the 24-hour clock (for example, 01:15:15).
m Zobrazí minuty jako číslo bez počátečních nul (například 12:1:15 ).Displays the minute as a number without leading zeros (for example, 12:1:15). Použijte %m v případě, že se jedná o jediný znak v uživatelsky definovaném číselném formátu.Use %m if this is the only character in your user-defined numeric format.
mm Zobrazí minuty jako číslo s počátečními nulami (například 12:01:15 ).Displays the minute as a number with leading zeros (for example, 12:01:15).
s Zobrazí sekundy jako číslo bez počátečních nul (například 12:15:5 ).Displays the second as a number without leading zeros (for example, 12:15:5). Použijte %s v případě, že se jedná o jediný znak v uživatelsky definovaném číselném formátu.Use %s if this is the only character in your user-defined numeric format.
ss Zobrazí sekundy jako číslo s počátečními nulami (například 12:15:05 ).Displays the second as a number with leading zeros (for example, 12:15:05).
f Zobrazí zlomky sekund.Displays fractions of seconds. Například ff zobrazí setiny sekundy, zatímco se ffff zobrazí deset sekundy sekund.For example ff displays hundredths of seconds, whereas ffff displays ten-thousandths of seconds. fV uživatelsky definovaném formátu můžete použít až sedm symbolů.You may use up to seven f symbols in your user-defined format. Použijte %f v případě, že se jedná o jediný znak v uživatelsky definovaném číselném formátu.Use %f if this is the only character in your user-defined numeric format.
t Používá 12hodinový čas a A pro každou hodinu před polednem zobrazí velká písmena; zobrazí velká písmena P za každou hodinu mezi poledne a 11:59 hodin.Uses the 12-hour clock and displays an uppercase A for any hour before noon; displays an uppercase P for any hour between noon and 11:59 P.M. Použijte %t v případě, že se jedná o jediný znak v uživatelsky definovaném číselném formátu.Use %t if this is the only character in your user-defined numeric format.
tt Pro národní prostředí, která používají 12 hodin, se zobrazí velká a malá písmena, a to v průběhu AM poledne; zobrazí velká písmena PM mezi poledne a 11:59 hodin.For locales that use a 12-hour clock, displays an uppercase AM with any hour before noon; displays an uppercase PM with any hour between noon and 11:59 P.M.

Pro národní prostředí, která používají 24hodinové hodiny, se nic nezobrazuje.For locales that use a 24-hour clock, displays nothing.
y Zobrazí číslo roku (0–9) bez počátečních nul.Displays the year number (0-9) without leading zeros. Použijte %y v případě, že se jedná o jediný znak v uživatelsky definovaném číselném formátu.Use %y if this is the only character in your user-defined numeric format.
yy Zobrazí rok ve dvouciferném číselném formátu s úvodní nulou, pokud lze použít.Displays the year in two-digit numeric format with a leading zero, if applicable.
yyy Zobrazí rok ve čtyřciferném číselném formátu.Displays the year in four-digit numeric format.
yyyy Zobrazí rok ve čtyřciferném číselném formátu.Displays the year in four-digit numeric format.
z Zobrazuje posun časového pásma bez úvodní nuly (například -8 ).Displays the timezone offset without a leading zero (for example, -8). Použijte %z v případě, že se jedná o jediný znak v uživatelsky definovaném číselném formátu.Use %z if this is the only character in your user-defined numeric format.
zz Zobrazuje posun časového pásma s úvodní nulou (například -08 ).Displays the timezone offset with a leading zero (for example, -08)
zzz Zobrazí úplný posun časového pásma (například). -08:00Displays the full timezone offset (for example, -08:00)

Příklad kódu starší verzeLegacy Code Example

Následují příklady uživatelsky definovaných formátů data a času pro December 7, 1958, 8:50 PM, 35 seconds :The following are examples of user-defined date and time formats for December 7, 1958, 8:50 PM, 35 seconds:

FormátFormat UvádíDisplays
M/d/yy 12/7/58
d-MMM 7-Dec
d-MMMM-yy 7-December-58
d MMMM 7 December
MMMM yy December 58
hh:mm tt 08:50 PM
h:mm:ss t 8:50:35 P
H:mm 20:50
H:mm:ss 20:50:35
M/d/yyyy H:mm 12/7/1958 20:50

Poznámky pro vývojáře inteligentního zařízeníSmart Device Developer Notes

Minimální doba překladu zařízení závisí na výrobci zařízení.The minimum time resolution for a device is determined by the manufacturer of the device. Pokud je časové rozlišení zařízení dostatečně hrubé, f znak formátu při spuštění na tomto zařízení vrátí hodnotu 0.If the time resolution for the device is coarse enough, the f format character returns 0 when run on that device.

Platí pro

Viz také