How to set up smartphones and PCs. Informational portal

How to get the current date 1s.

At some point, there is a need to work with variables of the "Date" type. In this article, we will look at the basic techniques - passing the current date, checking for an empty value, an arbitrary date.

When writing queries, it is often necessary to compare data with the current date. The built-in 1C language has the CurrentDate () function. It allows you to determine the current time and date on your computer. To perform operations with the current date, it is necessary to pass the value of this function as a parameter to the request.

Below is a query that selects all files attached to expense reports with a creation date up to now:

ExampleRequest = New Request;
ExampleRequest.Text = "
| CHOOSE
| Advance ReportAttached Files.Link
| FROM
| Reference.AvailableReportAttachedFilesASA AdvanceReportAttachedFiles
| WHERE
| Advance Report Attached Files.Date< &ТекДата»;
ExampleRequest.SetParameter ("CurrentDate", CurrentDate ());

Custom date

The above function allows you to compare and, therefore, make a selection for any period. This method allows you to specify a strict selection in the query without using additional parameters.

Note that using this function in the example above, we only passed three numbers (year, month, day) as input parameters. The last three (hour, minute, second) are optional and, if absent, are replaced with "0", that is, the beginning of the day.

For this example, you will receive all files attached to expense reports up to the end of last 2016. In this regard, we will indicate the hour, minute and second in order to compare with the moment in time "December 31, 2016 at 23:59:59".

SELECT
Advance ReportAttached Files.Link
FROM
Directory.AvailableReportAttachedFiles AS AdvanceReportAttachedFiles
WHERE
Advance Report Attached Files.Date< ДАТАВРЕМЯ(2016, 12, 31, 23, 59, 59)

Empty date

It is easiest to check a variable to see if it contains an empty date with a simple comparison. In this example, using a query, we will select all receipts of funds to a bank account, for which the incoming date is not filled.

Type: Number; Date; Boolean. The value to be formatted.<ФорматнаяСтрока> (optional) Type: String. The format string is a string value that includes formatting options. Formatting options are listed through the ";" symbol. The presence of the parameter means that the formatting differs from the standard one. If the parameter is not specified, then standard formatting is applied, which basically corresponds to converting the value to a string. However, for values ​​of type Number and Date by default (0 and 01.01.0001 00:00:00, respectively), an empty string will be returned. Each parameter is specified by the parameter name, the "=" symbol and the parameter value. The parameter value can be specified in single or double quotes. This is required if the parameter value contains characters used in the format string syntax. Arbitrary characters can be specified inside the parameter value, which will be displayed as is when forming the resulting string. Thus, for example, you can specify the separators for hours, minutes, and seconds when formatting time. If these characters match the format string values, they must be enclosed in single quotes. The names and values ​​of the format string parameters: Л (L) - the name of the language, country for which standard formatting will be performed. Available language names are located after the format string description. ND is the total number of displayed decimal places for whole and fractional parts. In this case, the original number is rounded in accordance with the rules for rounding off OKr15 to 20. If this parameter is specified, then to display the fractional part of a number, it is necessary to indicate the NPV parameter, otherwise the fractional part will not be displayed. NFD - the number of decimal places in the fractional part. In this case, the original number is rounded in accordance with the rules for rounding off OKr15 to 20. ChS (NS) - shift of digits: positive - division, negative - multiplication. In other words, this means that the original number will be multiplied or divided by 10 * С, where С is the modulo value of the parameter. RLD (NDS) - the character separator of integer and fractional parts. ChRG (NGS) - a character separator of groups of integer part of a number. If you use an empty string as a separator, then the non-breaking space character will be the separator. CHN (NZ) is a string representing the zero value of a number. If not specified, then the empty string representation. If "CHN =" is specified, then in the form of "0". Not used for numeric input fields. CHVN (NLZ) - whether to output leading zeros. The value of this parameter is not specified, the presence of the parameter itself determines the output of leading zeros. ЧГ (NG) - the order of grouping the digits of the number. The values ​​are numbers, separated by commas, indicating the number of digits to be grouped from right to left. Only the first two numbers make sense. The first of them indicates the primary grouping, that is, the one that will be used for the least significant digits of the integer part of the number. If the second number is not specified, then only the least significant digits will be grouped. If 0 is specified as the second number, then the value specified for the primary grouping will be applied to all digits of the integer part of the number. If a value other than 0 is used as the second number, then this value will be used to group all the digits, except for the least significant ones already grouped. CHO (NN) - represent negative numbers. 0 (0) - a string like "(1,1)"; 1 (1) - a string like "-1,1"; 2 (2) - a string like "- 1,1"; 3 (3) - a string like "1,1-"; 4 (4) - a string like "1,1 -". DF (DF) - date format. d (d) - day of the month (in digits) without a leading zero; dd (dd) - day of the month (in digits) with a leading zero; ddd (ddd) - short name of the day of the week *); dddd (dddd) - full name of the day of the week *); М (M) - month number (in digits) without a leading zero; MM (MM) - month number (in digits) with a leading zero; MMM (MMM) - short name of the month *); MMMM (MMMM) - full name of the month *); к (q) - number of a quarter in a year; г (y) - number of the year without century and leading zero; yy (yy) - year number without century with leading zero; yyyy (yyyy) - year number with century; h (h) - hour in 12 hour version without leading zeros; hh (hh) - hour in 12 hour variant with leading zero; H (H) - hour in the 24-hour variant without leading zeros; HH (HH) - hour in 24-hour variant with leading zero; m (m) - minute without leading zero; mm (mm) - minute with a leading zero; s (s) - second without leading zero; ss (ss) - second with a leading zero; bc (tt) - displays half day AM / PM (valid only for configuration languages ​​that support 12 hour clock). Note: *) - is not used for the format string of the date input field. DLF (DLF) - local date format. Specifies the display option for date parts. D (D) - date (in numbers); DD (DD) - long date (month in words); B (T) - full time, date can be combined with time; DV (DT) - date time. Example: The expression Format (‘20051120140323’, “DLF = DDV”) has the value “Nov 20, 2005 14:03:23 ″. Important! Order of format string options for DLF<дата + время> (DV or DDV) cannot be changed. DP (DE) is a string representing an empty date (for example, Format ('00010101000000', "DP =" "empty date" "") will return the string "empty date"). BL (BF) - a string representing the Boolean value False. BI (BT) - a string representing the logical value True. Note. Using parameters for the input field: parameter DP (DE) is not used; parameter DLF (DLF) is used only with the values ​​of D (D), V (T) and DV (DT). Returned value: Type: String. The string resulting from the formatting of the passed value. Description: Provides an easy-to-read representation of values. Useful for reports and other visual display of values. Availability: Thin Client, Web Client, Server, Fat Client, Outside Connection, Mobile App (Client), Mobile App (Server). List of languages: af Afrikaans af_NA (af_NA) Afrikaans (Namibia) af_ZA Afrikaans (South Africa) am Amharic am_ET Amharic (Ethiopia) ar Arabic ar_AE Arabic (United Arab Emirates) ar_BH Arabic (Bahrain) ar_DZ Arabic (Algeria) ar_EG Arabic (Egypt) ar_IQ Arabic (Iraq) ar_JO Arabic (Jordan) ar_KW Arabic (Kuwait) ar_LB Arabic (Lebanon) ar_LY Arabic (Libya) ar_MA Arabic (Morocco) ar_OM Arabic (Oman) ar_QA Arabic (Qatar) ar_SA Arabic (Saudi Arabia) (ar_SA) ) ar_SY Arabic (Syria) ar_TN Arabic (Tunisia) ar_YE Arabic (Yemen) as Assamese as_IN Assamese (India) az Azeri az_AZ Azeri (Azerbaijan) az_Cyrl Azeri (Cyrillic) az_Cyrl_AZ Azeri (Cyrillic, Azerbaijan) az_Latn Azeri (Latin_AZ) Latin, Azerbaijan) be Belarusian be_BY Belarusian (Belarus) bg Bulgarian bg_BG Bulgarian (Bulgaria) bn Bengali bn_IN Bengali (India) ca Catalan ca_ES Catalans cue (Spain) cs Czech cs_CZ Czech (Czech Republic) cy Welsh cy_GB Welsh (UK) da Danish da_DK Danish (Denmark) de German de_AT German (Austria) de_BE German (Belgium) de_CH German (Switzerland) de_DE German (Germany) de_LI ( de_LI) German (Liechtenstein) de_LU German (Luxembourg) el Greek el_CY (el_CY) Greek (Cyprus) el_GR Greek (Greece) en English en_AU English (Australia) en_BE English (Belgium) en_BW English (Botswana) en_BZ (en_BZ) English (Belize) ) en_CA English (Canada) en_GB English (UK) en_HK English (Hong Kong, (SAR, China)) en_IE English (Ireland) en_IN English (India) en_JM (en_JM) English (Jamaica) en_MH (en_MH) English ( Marshall Islands) en_MT English (Malta) en_NA (en_NA) English (Namibia) en_NZ English (New Zealand) en_PH English (Philippines) en_PK English (Pakistan) en_SG English (Singapore) en_TT (en_TT) English th (Trinidad and Tobago) en_US English (United States of America) en_VI English (US Virgin Islands) en_ZA English (South Africa) en_ZW English (Zimbabwe) es Spanish es_AR Spanish (Argentina) es_BO Spanish (Bolivia) es_CL Spanish (Chile) es_CO Spanish (Colombia) es_CR Spanish (Costa Rica) es_DO Spanish (Dominican Republic) es_EC Spanish (Ecuador) es_ES Spanish (Spain) es_GT Spanish (Guatemala) es_HN Spanish (Honduras) es_MX Spanish (Mexico) es_NI Spanish (Nicaragua) Spanish (Panama) ) es_PE Spanish (Peru) es_PR Spanish (Puerto Rico) es_PY Spanish (Paraguay) es_SV Spanish (El Salvador) es_US Spanish (United States of America) es_UY Spanish (Uruguay) es_VE Spanish (Venezuela) et Estonian et_EE Estonian (Estonia) eu_ES Basque Basque (Spain) fa Persian fa_AF Persian (Afghanistan) fa_IR Persian (Iran) fi Finnish fi_FI Finnish (Finland) fo Faroese fo_FO Faroese (Faroe Islands) fr French fr_BE French (Belgium) fr_CA French (Canada) fr_CH French (Switzerland) fr_FR French (France) fr_LU French (Luxembourg) fr_MC (fr_MC) French (Monaco) fr_SN (fr_SN) French (Senegal) ga Irish ga_IE Irish (Ireland) gl Galician gl_ES, Galician (Spain) gu Guyarati gu_IN Guyarati (India) he Hebrew he_IL Hebrew (Israel) hi Hindi hi_IN Hindi (India) hr Croatian hr_HR Croatian (Croatia) hu Hungarian hu_HU Hungarian (Hungary) hy Armenian Armenia hy_VEDAM Armenian_ Armenian (Armenia, REVISED) id Indonesian id_ID Indonesian (Indonesia) is Icelandic is_IS Icelandic (Iceland) it Italian it_CH, Italian (Switzerland) it_IT Italian (Italy) ja Japanese ja_JP Japanese (Japan) ka Georgian ka_GE Georgian (Georgia) kk Kazakh kk_KZ Kazakh (Kazakhstan) kl Greenlandic kl_GL Greenlandic (Greenland) kn Kannada kn_IN Kannada (India) ko Korean ko_KR Korean (South Korea) kok Konkani kok_IN Konkani (Indus iya) ky Kyrgyz ky_KY Kyrgyz (Kyrgyzstan) lt Lithuanian lt_LT Lithuanian (Lithuania) lv Latvian lv_LV Latvian (Latvia) mk Macedonian mk

When working with 1C dates, the typical order of the date parts is year, month, day, hour, minutes, seconds. In this case, hours, minutes, seconds can be skipped.

When creating a date from a string ("cast to date"), you can specify in the localized format (day.month.year hours: minutes: seconds), but only completely.

For instance:
// Working with 1C dates - convert a date to 1C from parts - year, month, day (plus optional time)
Date = Date (2012,10,30); // no time
Date = Date (2012,10,30,12,00,00); //with time

// Working with 1C dates - convert a date to 1C from a string, different ways
Date = Date ("20121030"); // year, month, day
Date = Date ("10/30/2012 12:00:00"); // localized format, only in full

// Working with 1C dates - specifying the date value without casting, directly
Date = "20121030"; // no time
Date = "20121030120000"; //with time

Working with dates 1C - Empty date 1C

To check the 1C date for fullness - it is compared with the "empty date". If there is a date type attribute in the reference book / document, if the user has not filled in this field, then its value will also be “empty date”.

"Empty date" is 01.01.0001 00:00:00.

For instance:
EmptyDate = "00010101000000";
If RequiredDate = "00010101000000" Then
Report ("You did not fill in a very necessary date");
EndIf;

Working with dates 1C - Date in the details (reference books, documents, etc.)

When specifying the type of attribute, you can specify the use of:

  • Date only (time is then always 00:00:00)
  • Time only (date is then always 01.01.0001)
  • Date and time

Getting the date

To get the date and time, use the 1C CurrentDate () function.

The very important place is where this function is called - on the client or on the server. For details, see the topic "Performance Mode / Performance". It often happens that the time is slightly different on the client machines, so they try to use the server time everywhere - even if it is not set correctly on the server, then at least all clients will have the same wrong time.

In order to get the server date (the date set in the operating system of the server computer), a common module is usually created in the configuration with the "Server" checkbox checked in the properties, and the function is created in it
// the function is located in a common module, for example, named ServerFunctions
// in the properties of the common module, the "Server" checkbox is checked and the "Client" checkbox is not set
Function GetServerDate () Export
Return CurrentDate ();
EndFunction

// calling this function for use from another module looks like this
DocumentObject.Date = ServerFunctions.GetServerDate (); //ModuleName.FunctionName ()

Also, in the thin client, directly next to the functions of the modules, it is indicated where it will be executed:

Start and end of the day

For the date "10/30/2012":

  • start date of the day looks like this "10/30/2012 00:00:00"
  • end date looks like this "10/30/2012 23:59:59"

It is used in reports and queries that require data for a period - day, month, year.

So, for example, the period from 01/01/2012 00:00:00 to 01/31/2012 00:00:00 is incorrect, because it does not include one day of the month (but it does include one second of the last day of the month).

Working with 1C dates - Comparing dates

Date contains date and time. When comparing dates (excluding time), they are usually brought to the beginning of the day (month, year).

For instance:
Date1 = Date ("10/30/2012 12:00:00");
If StartDay (Date1) = StartDay (DocumentRef.Date) Then
Report ("The document was entered by the specified date");
EndIf;

Just in case, an example of comparing dates in a period:
If DocumentRef.Date> = StartMonth (CurrentDate ()) and
DocumentRef.Date

Working with dates 1C - Changing the date

The date is the number of seconds. If we want not only to find out whether one date is greater than another, but also how much more, then we get the difference in seconds.

For instance:
DayStart = CurrentDate () - DayStart (CurrentDate ());
Report ("Since the beginning of the day has passed" + String (From the beginning of the day) + "seconds");
Report ("Since the beginning of the day has passed" + String (From the beginning of the day / 60) + "minutes");
Report ("Since the beginning of the day has passed" + String (From the beginning of the day / 60/60) + "hours");

We can also change the date, when changing, we add or subtract the number of seconds:
StartThisDay = StartDay (CurrentDate ());

BeginningPreviousDay = BeginningDay (BeginningThisDay - 1); // remove the second - doing "yesterday" and take the beginning of the day from "yesterday"

StartPreviousDay = StartThisDay - 24 * 60 * 60; // another way - we subtract 24 hours - 24 (hours) * 60 (minutes turned out) * 60 (seconds)

Working with dates 1C - Moment of time

A point in time is an extended date representation applicable to documents (and accordingly registers).

It is required to compare the time of documents if the date and time of the documents are the same. Accordingly, it can be used when filtering in queries.

A moment in time can be obtained from a document in the following ways:
// method 1
DocumentMomentTime = DocumentRef.Timepoint ();

You can also compare a point in time with a date / time:
Moment of TimeStatus = New Moment of Time (Start of Day (CurrentDate ()));
If DocumentRef.MomentTime (). Compare (TimeTimeStandard) = -1 Then
Report ("The document was introduced earlier than today");
EndIf;
// If the document was entered by today's date at 00:00:00, then it was entered anyway - today

Working with dates 1C - Date formatting

The "Date" type in 1C is one of the 4 main data types along with number, string and boolean. Dates are ubiquitous in configurations - it is impossible to avoid working with this data type during development. Therefore, it is better to start writing queries, already understanding how to handle dates, what are the possibilities for working with them, how they are stored. Let's look at examples of all the nuances of writing requests with different dates.

Examples of working with dates in 1C requests

First of all, you need to get the date in the request in the required format - with or without time. There are several ways to accomplish this task:

  1. Pass through a parameter. You can get the current date of the session only by this method;
  2. Get the date in the request from the selection field;
  3. Convert from numeric values ​​using the DATE TIME () function.

The most common task when working with documents is to check for an empty date in a 1C request. In this case, the easiest way is to compare a variable or field with an empty date, which is obtained using the DATE TIME (1,1,1) function:

DATE TIME (1, 1, 1)

A similar command can get an arbitrary date and time in a request. At the same time, they can be specified with an accuracy of a second by setting 6 numbers as parameters. If only 3 numbers are used, then hours, minutes and seconds will be set to 0 (the beginning of the day). For example, we need to select documents for the first 10 days of January 2018 in the request:

SELECT Receipt to Settlement Account.Ref AS Link FROM Document.Choose to Settlement Account AS Receipt to Settlement Account WHERE Receipt to Settlement Account.Date BETWEEN DATE TIME (2018, 1, 1, 0, 0, 0) AND DATE, 1, 23, 10

In a query in the built-in 1C language, you can not only select various fields and receive parameters. There are many functions that make it easy to format the date for a specific task. If you often work with dates in a query, then you should know these commands:

  • BEGINNING OF PERIOD. As parameters, the date and time interval are indicated, in the context of which it is necessary to get the beginning of the date. Used to convert a date to a timeless format. For this it is necessary to set the second parameter - "DAY";
PERIOD START (,) PERIOD START (& Date, DAY) period> date>
  • END OF PERIOD. A similar command that returns the last date in terms of the units specified in the parameters;
  • ADDEDDATE. Allows you to get a date greater than the specified number of specified time units. The date, time unit and number are specified as parameters of the function;
ADDEDDATE (,) ADDEDATE (& Date, DAY, 10) count> type> date>
  • DIFFERENCE Gets the difference between dates in the specified units;
DIFFERENCE DATE (,) DIFFERENCE DATE (& Date1, & Date2, DAY) type> date2> date1>
  • DAY OF THE WEEK. Returns the ordinal number of one of the days of the week.

Applying these functions correctly, the developer can solve rather non-trivial tasks. For example, getting the name of the day of the week of the current date in a query as a string:

SELECT WHEN DAY WEEK (& CurrentDate) = 1 THEN "Monday" WHEN DAY WEEK (& CurrentDate) = 2 THEN "Tuesday" WHEN DAY WEEK (& CurrentDate) = 3 THEN "Wednesday" WHEN DAY WEEK Current (& CurrentDate) = 4 THEN "DAY" & Thursday = 5 THEN "Friday" WHEN DAY WEEK (& CurrentDate) = 6 THEN "Saturday" ELSE "Sunday" END

Converting types in a 1C query from a number or a string to a date is a laborious task. From numbers, you can get a date using the DATE TIME function, from a string - by combining the SUBSTRING function and the SELECT WHEN THEN ELSE construct. Based on this, developers prefer to get the date from other types in the module and pass it to the request using a parameter. Unfortunately, this is not always feasible, so you have to change the date format in the request.

It is possible to specify the date in the 1C request as a parameter for retrieving data from virtual register tables. All of the above functions can also be used in this role. But here it is important not to allow the empty date in the 1C request to affect the final result of the code execution, therefore, it is imperative to do a check.

Top related articles