diff --git a/docs/reference/javascript-cheatsheet.md b/docs/reference/javascript-cheatsheet.md index 5e245b54d..7375b0bd2 100644 --- a/docs/reference/javascript-cheatsheet.md +++ b/docs/reference/javascript-cheatsheet.md @@ -14,69 +14,120 @@ The JavaScript [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re **Note:** The outputs below show the date and time when this documentation was created. The date and time will be different for you. +**Note:** We are using Set node for illustration here. You can use the code snippets as an expression in any node. + ### 1. Get current time To use the built-in methods for managing and formatting date in JavaScript, you have to create a date object. The `Date()` constructor returns the date and time in the [ISO](https://en.wikipedia.org/wiki/ISO_8601) format. To get today's date use the following snippet +#### Function node + ```js const today = new Date(); +items[0].json.today = today; +return items; ``` + +The output will be similar to the following image. +![Get current time using the code snippet in a Function node](./js-snippets/new_date_snippet.png) + +#### Set node + + ```js -// Output -"2020-10-09T09:45:36.593Z" -``` +{{new Date()}} +``` + +The output will be similar to the following image. +![Get current time using the expression in a Set node](./js-snippets/new_date_expression.png) ### 2. Get Date Use the `getDate()` method to get the date. +#### Function node ```js const today = new Date(); const date = today.getDate(); +items[0].json.today_date = date; +return items; ``` +The output will be similar to the following image. +![Get today's date using the code snippet in a Function node](./js-snippets/today_date_snippet.png) + +#### Set node ```js -// Output -9 +{{new Date().getDate()}} ``` +The output will be similar to the following image. +![Get today's date using the expression in a Set node](./js-snippets/today_date_expression.png) ### 3. Get Month In JavaScript, the month is 0-indexed. For example, January has index 0, February has index 1, and so on. Use the `getMonth()` method to get the month. +#### Function node ```js const today = new Date(); -const month = today.getMonth(); -``` -```js -// Output -9 // 0-index +const month = today.getMonth()+1; +items[0].json.month = month; +return items; ``` +The output will be similar to the following image. +![Get month using the code snippet in a Function node](./js-snippets/month_snippet.png) + +Use the following code snippet to get the month from the month's array. ```js const today = new Date(); const month = today.getMonth(); const months = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; const current_month = months[month]; +items[0].json.current_month = current_month; +return items; ``` +The output will be similar to the following image. +![Get current month using the code snippet in a Function node](./js-snippets/current_month_snippet.png) + +#### Set node ```js -// Output -'Oct' +{{new Date().getMonth()+1}} ``` +The output will be similar to the following image. +![Get month using the expression in a Set node](./js-snippets/month_expression.png) + ### 4. Perform calculations -You can perform calculations on the data object to get the required date or time. +You can perform calculations on the data object to get the required date or time. JavaScript returns the time in milliseconds. To perform calculations on date and time, we convert the milliseconds to the appropriate unit. For example, in the code snippets below we are converting the unit of time from milliseconds into days. +#### Function node ```js const today = new Date(); -const yesterday = new Date(new Date(today).getTime()-(1*24*60*60*1000)) -const tomorrow = new Date(new Date(today).getTime()+(1*24*60*60*1000)) +const yesterday = new Date(new Date(today).getTime()-(1*24*60*60*1000)); +const tomorrow = new Date(new Date(today).getTime()+(1*24*60*60*1000)); +return [{json:{yesterday:yesterday, today:today, tomorrow:tomorrow}}]; ``` +The output will be similar to the following image. +![Get date and time for yesterday, today and tomorrow, using the code snippet in a Function node](./js-snippets/calculations_snippet.png) + +#### Set node + +Today ```js -// Output -"2020-10-09T09:45:36.593Z" // today -"2020-10-08T09:45:36.593Z" // yesterday -"2020-10-10T09:45:36.593Z" // tomorrow -``` \ No newline at end of file +{{new Date()}} +``` + +Yesterday +```js +{{new Date(new Date().getTime()-(1*24*60*60*1000));}} +``` + +Tomorrow +```js +{{new Date(new Date().getTime()+(1*24*60*60*1000));}} +``` +The output will be similar to the following image. +![Get date and time for yesterday, today and tomorrow, using the expression in a Set node](./js-snippets/calculation_expression.png) \ No newline at end of file diff --git a/docs/reference/js-snippets/calculation_expression.png b/docs/reference/js-snippets/calculation_expression.png new file mode 100644 index 000000000..5b975c936 Binary files /dev/null and b/docs/reference/js-snippets/calculation_expression.png differ diff --git a/docs/reference/js-snippets/calculations_snippet.png b/docs/reference/js-snippets/calculations_snippet.png new file mode 100644 index 000000000..4cb60c2ff Binary files /dev/null and b/docs/reference/js-snippets/calculations_snippet.png differ diff --git a/docs/reference/js-snippets/current_month_snippet.png b/docs/reference/js-snippets/current_month_snippet.png new file mode 100644 index 000000000..d9baa020c Binary files /dev/null and b/docs/reference/js-snippets/current_month_snippet.png differ diff --git a/docs/reference/js-snippets/month_expression.png b/docs/reference/js-snippets/month_expression.png new file mode 100644 index 000000000..4f5967666 Binary files /dev/null and b/docs/reference/js-snippets/month_expression.png differ diff --git a/docs/reference/js-snippets/month_snippet.png b/docs/reference/js-snippets/month_snippet.png new file mode 100644 index 000000000..57bb56d5c Binary files /dev/null and b/docs/reference/js-snippets/month_snippet.png differ diff --git a/docs/reference/js-snippets/new_date_expression.png b/docs/reference/js-snippets/new_date_expression.png new file mode 100644 index 000000000..032cb434a Binary files /dev/null and b/docs/reference/js-snippets/new_date_expression.png differ diff --git a/docs/reference/js-snippets/new_date_snippet.png b/docs/reference/js-snippets/new_date_snippet.png new file mode 100644 index 000000000..a5ba852ac Binary files /dev/null and b/docs/reference/js-snippets/new_date_snippet.png differ diff --git a/docs/reference/js-snippets/today_date_expression.png b/docs/reference/js-snippets/today_date_expression.png new file mode 100644 index 000000000..eccc9d510 Binary files /dev/null and b/docs/reference/js-snippets/today_date_expression.png differ diff --git a/docs/reference/js-snippets/today_date_snippet.png b/docs/reference/js-snippets/today_date_snippet.png new file mode 100644 index 000000000..e8cfa3019 Binary files /dev/null and b/docs/reference/js-snippets/today_date_snippet.png differ