Strings and dates
String functions, splitting and joining strings, number and date formatting, date arithmetic.
11 questions
JuniorTheoryVery commonHow do you output a number and a date in the exact format you need?
How do you output a number and a date in the exact format you need?
With Формат(Значение, ФорматнаяСтрока), which returns a string. The format string lists name=value pairs separated by ;: "ЧДЦ=2; ЧГ=0" gives two decimals without digit groups, and "ДФ=dd.MM.yyyy" gives a date like 05.11.2024.
Common mistakes
- ✗Expecting
Форматto change the number itself: it returns a new string - ✗Writing spreadsheet masks such as
#,##0.00: the format string takes parameters likeЧДЦandДФ - ✗Writing the month as
mminДФ:mmmeans minutes, and the month isMM
Follow-up questions
- →What does
Формат(0, "ЧДЦ=2")return, and how do you show a zero? - →How do you print a number with a fixed width and leading zeros?
JuniorTheoryVery commonHow do you split a string into parts and then join the parts back together?
How do you split a string into parts and then join the parts back together?
СтрРазделить(Строка, Разделитель) returns an array of parts and keeps empty ones unless the third parameter is Ложь; each character of the separator splits on its own. СтрСоединить(Массив, Разделитель) joins the array back into one string.
Common mistakes
- ✗Expecting empty parts to vanish by default: pass
Ложьas the third parameter to drop them - ✗Passing a comma and a space as one separator: each of the two characters splits the string, so empty parts appear
- ✗Gluing parts with
+in a loop:СтрСоединитьdoes it in one call
Follow-up questions
- →How do you trim the spaces around each part after splitting?
- →How would you split a multi-line text into separate lines?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
29.02.2024 31.01.2024 23:59
Open full question →Common mistakes
- ✗Expecting 31 January plus a month to spill into March:
ДобавитьМесяцclamps the date to the last day of February - ✗Expecting
КонецМесяцаto return the start of the last day: it returns its last second, 23:59:59
Follow-up questions
- →What does
ДобавитьМесяц('20240229', 12)return? - →How do you get the first day of the next month from any date?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
30.11.2024 259200
Open full question →Common mistakes
- ✗Expecting
Продажа + 3to move the date by three days: a date adds seconds, so three days are3 * 86400 - ✗Expecting the difference of two dates in days: it is in seconds, and days come from dividing by 86400
Follow-up questions
- →How do you add three days to a date?
- →How do you count calendar days between two dates that carry a time?
JuniorTheoryCommonHow do you count the number of days between two dates in the 1C language?
How do you count the number of days between two dates in the 1C language?
Subtracting one date from another gives the difference in seconds, so divide it by 86400, the seconds in a day. If the dates carry a time, cut it off first with НачалоДня: (НачалоДня(Дата2) - НачалоДня(Дата1)) / 86400 gives whole days.
Common mistakes
- ✗Expecting
Дата2 - Дата1to give days: it gives seconds - ✗Dividing the difference of dates with different times of day: the result is fractional unless both go through
НачалоДня - ✗Looking for a
РазностьДатfunction in the built-in language: it exists only in the query language
Follow-up questions
- →How do you add three days to a date in the built-in language?
- →How do you count the number of full months between two dates?
JuniorTheoryCommonWhat does the program print?
What does the program print?
1 234,50 / 1 235 / 1234,5
Open full question →Common mistakes
- ✗Expecting
ЧДЦ=0to drop the fraction:Форматrounds, so 1234.5 becomes 1 235 - ✗Expecting a dot before the fraction, as in the program text:
Форматputs a comma there and spaces between digit groups
Follow-up questions
- →Why does
Формат(0, "ЧДЦ=2")return an empty string? - →How do you print a receipt number with leading zeros through
Формат?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
10 10+20+30
Open full question →Common mistakes
- ✗Counting
""as two characters: a doubled quote in a literal is one quote in the text - ✗Expecting
СтрЗаменитьto replace only the first occurrence: it replaces all of them
Follow-up questions
- →How do you write a string that spans several lines of code?
- →How do you count how many hyphens a string holds?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
4 2 ручка+ластик
Open full question →Common mistakes
- ✗Expecting empty parts to be dropped by default: without the third parameter
СтрРазделитьkeeps them - ✗Forgetting that a comma at the end also gives an empty part: an empty string is left after the last comma
Follow-up questions
- →How do you remove the spaces around each part after splitting?
- →How do you split a string by several separators at once?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
9 3 0042
Open full question →Common mistakes
- ✗Counting string positions from 0, like array indexes:
СтрНайтиandСредcount characters from 1 - ✗Taking the third parameter of
Средfor the end position: it is the length of the substring
Follow-up questions
- →What does
СтрНайтиreturn when the substring is absent? - →How do you take the part of
Артикулafter the last hyphen?
MiddleTheoryCommonWhy does Формат(0, "ЧДЦ=2") return an empty string, and how do you show a zero?
Why does Формат(0, "ЧДЦ=2") return an empty string, and how do you show a zero?
Формат treats zero as an empty value and prints the text of its ЧН parameter instead, empty by default: "ЧДЦ=2; ЧН=0,00" prints 0,00. The empty date Дата(1, 1, 1) works the same way and gives an empty string unless ДП sets the text.
Common mistakes
- ✗Expecting
Форматto print 0,00 for a zero amount: withoutЧНit prints nothing - ✗Expecting an empty date to print as 01.01.0001:
Форматgives an empty string unlessДПis set
Follow-up questions
- →What does
ЧН=with no text after the sign print for a zero? - →How would you print «нет даты» for an empty date in a report column?
MiddleTheoryCommonHow do you write a string literal that holds quotes and line breaks in 1C?
How do you write a string literal that holds quotes and line breaks in 1C?
A quote inside a string is doubled: "Чек ""А-12""". A literal can span lines if each next line starts with |, and the breaks stay in the text; in code, Символы.ПС adds one. СтрРазделить(Текст, Символы.ПС) splits it back into lines.
Common mistakes
- ✗Escaping a quote with a backslash: inside a 1C string a quote is doubled
- ✗Expecting the
|lines to be glued together: each line break stays in the text
Follow-up questions
- →How do you count the lines of a multi-line text and read one of them?
- →Why do query texts in 1C code usually use a multi-line literal?