MySQL Date and Time functions

https://www.w3resource.com/mysql/date-and-time-functions/date-and-time-functions.php

Date and Time functions

FunctionsDescription
ADDDATE()MySQL ADDDATE() adds a time value with a date.
ADDTIME()In MySQL the ADDTIME() returns a time or datetime after adding a time value with a time or datetime.
CONVERT_TZ()In MySQL the CONVERT_TZ() returns a resulting value after converting a datetime value from a time zone specified as the second argument to the time zone specified as the third argument.
CURDATE()In MySQL the CURDATE() returns the current date in 'YYYY-MM-DD' format or 'YYYYMMDD' format depending on whether numeric or string is used in the function.
CURRENT_DATE()In MySQL the CURRENT_DATE returns the current date in ‘YYYY-MM-DD’ format or YYYYMMDD format depending on whether numeric or string is used in the function.
CURRENT_TIME()In MySQL the CURRENT_TIME() returns the current time in ‘HH:MM:SS’ format or HHMMSS.uuuuuu format depending on whether numeric or string is used in the function.
CURRENT_TIMESTAMP()In MySQL the CURRENT_TIEMSTAMP returns the current date and time in ‘YYYY-MM-DD HH:MM:SS’ format or YYYYMMDDHHMMSS.uuuuuu format depending on whether numeric or string is used in the function.
CURTIME()In MySQL the CURTIME() returns the value of current time in ‘HH:MM:SS’ format or HHMMSS.uuuuuu format depending on whether numeric or string is used in the function.
DATE_ADD()MySQL DATE_ADD() adds time values (as intervals) to a date value. The ADDDATE() is the synonym of DATE_ADD().
DATE_FORMAT()MySQL DATE_FORMAT() formats a date as specified in the argument. A list of format specifiers given bellow may be used to format a date.
DATE_SUB()MySql date_sub() function subtract a time value (as interval) from a date.
DATE()MySQL DATE() takes the date part out from a datetime expression.
DATEDIFF()MySQL DATEDIFF() returns the number of days between two dates or datetimes.
DAY()MySQL DAY() returns the day of the month for a specified date.
DAYNAME()MySQL DAYNAME() returns the name of the week day of a date specified in the argument.
DAY OF MONTH()MySQL DAYOFMONTH() returns the day of the month for a given date.
DAY OF WEEK()MySQL DAYOFWEEK() returns the week day number (1 for Sunday,2 for Monday …… 7 for Saturday ) for a date specified as an argument.
DAY OF YEAR()MySQL DAYOFYEAR() returns day of the year for a date. The return value is within the range of 1 to 366.
EXTRACT()MySQL EXTRACT() extracts a part of a given date.
FROM_DAYS()MySQL FROM_DAYS() returns a date against a datevalue.
FROM_UNIXTIME()MySQL FROM_UNIXTIME() returns a date /datetime from a version of unix_timestamp.
GET_FORMAT()MySQL GET_FORMAT() converts a date or time or datetime in a formatted manner as specified in the argument.
HOUR()MySQL HOUR() returns the hour of a time.
LAST_DAY()MySQL LAST_DAY() returns the last day of the corresponding month for a date or datetime value.
LOCALTIME()MySQL LOCALTIME returns the value of current date and time in ‘YYYY-MM-DD HH:MM:SS’ format or YYYYMMDDHHMMSS.uuuuuu format depending on the context (numeric or string) of the function.
LOCALTIMESTAMP()MySQL LOCALTIMESTAMP returns the value of current date and time in ‘YYYY-MM-DD HH:MM:SS’ format or YYYMMDDHHMMSS.uuuuuu format depending on the context (numeric or string) of the function.
MAKEDATE()MySQL MAKEDATE() returns a date by taking a value of a year and a number of days. The number of days must be greater than 0 otherwise a NULL will be returned.
MAKETIME()MySQL MAKETIME() makes and returns a time value from a given hour, minute and seconds.
MICROSECOND()MySQL MICROSECOND() returns microseconds from the time or datetime expression.
MINUTE()MySQL MINUTE() returns a minute from a time or datetime value.
MONTH()MySQL MONTH() returns the month for the date within a range of 1 to 12 ( January to December).
MONTHNAME()MySQL MONTHNAME() returns the full name of the month for a given date.
NOW()MySQL NOW() returns the value of current date and time in ‘YYYY-MM-DD HH:MM:SS’ format or YYYYMMDDHHMMSS.uuuuuu format depending on the context (numeric or string) of the function.
PERIOD_ADD()MySQL PERIOD_ADD() adds a number of months with a period and returns the value in the format YYYYMM OR YYMM. Remember that the format YYYYMM and YYMM are not date values.
PERIOD_DIFF()MySQL PERIOD_DIFF() returns the difference between two periods.
QUARTER()MySQL QUARTER() returns the quarter of the year for a date.
SEC_TO_TIME()MySQL SEC_TO_TIME() returns a time value by converting the seconds specified in the argument.
SECOND()MySQL SECOND() returns the second for a time.
STR_TO_DATE()MySQL STR_TO_DATE() returns a datetime value by taking a string and a specific format string as arguments.
SUBDATE()MySQL SUBDATAE() subtracts a time value (as interval) from a given date.
SUBTIME()MySQL SUBTIME() subtracts one datetime value from another.
SYSDATE()MySQL SYSDATE() returns the current date and time in YYYY-MM-DD HH:MM:SS or YYYYMMDDHHMMSS.uuuuuu format depending on the context of the function.
TIME_FORMAT()MySQL TIME_FORMAT() converts a time in a formatted string using the format specifiers.
TIME_TO_SEC()MySQL TIME_TO_SEC() converts a time value in to seconds.
TIME()MySQL TIME() extracts the time part of a time or datetime expression as string format.
TIMEDIFF()MySQL TIMEDIFF() returns the differences between two time or datetime expressions.
TIMESTAMP()MySQL TIMESTAMP() returns a datetime value against a date or datetime expression.
TIMESTAMPADD()MySQL TIMESTAMPADD() adds time value with a date or datetime value.
TIMESTAMPDIFF()MySQL the TIMESTAMPDIFF() returns a value after subtracting a datetime expression from another.
TO_DAYS()MySQL TO_DAYS() returns number of days between a given date and year 0.
UNIX_TIMESTAMP()MySQL UNIX_TIMESTAMP() returns a Unix timestamp in seconds since '1970-01-01 00:00:00' UTC as an unsigned integer if no arguments are passed with UNIT_TIMESTAMP().
UTC_DATE()MySQL UTC_DATE returns the current UTC (Coordinated Universal Time) date as a value in 'YYYY-MM-DD' or YYYYMMDD format depending on the context of the function i.e. in a string or numeric context.
UTC_TIME()MySQL UTC_TIME returns the current UTC time as a value in 'HH:MM:SS' or HHMMSS format depending on the context of the function i.e. in a string or numeric context.
UTC_TIMESTAMP()In MySQL the UTC_TIMESTAMP returns the current UTC date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format depending on the usage of the function i.e. in a string or numeric context.
WEEK()MySQL WEEK() returns the week number for a given date.
WEEKDAY()MySQL WEEKDAY() returns the index of the day in a week for a given date (0 for Monday, 1 for Tuesday and ......6 for Sunday).
WEEK OF YEAR()MySQL WEEKOFYEAR() returns the calender week (as a number) of a given date.
YEAR()MySQL YEAR() returns the year for a given date.
YEARWEEK()MySQL YEARWEEK() returns year and week number for a given date.


Repeat HTML element multiple times using ngFor based on a number

Source: https://stackoverflow.com/questions/36535629/repeat-html-element-multiple-times-using-ngfor-based-on-a-number

<ng-container *ngFor="let i of [].constructor(20)">🐱</ng-container>

What is class=“mb-0” in Bootstrap 4?

Source: https://stackoverflow.com/questions/41574776/what-is-class-mb-0-in-bootstrap-4

Bootstrap has a wide range of responsive margin and padding utility classes. They work for all breakpoints:
xs (<=576px), sm (>=576px), md (>=768px), lg (>=992px) or xl (>=1200px))
The classes are used in the format:
{property}{sides}-{size} for xs & {property}{sides}-{breakpoint}-{size} for sm, md, lg, and xl.
m - sets margin
p - sets padding

t - sets margin-top or padding-top
b - sets margin-bottom or padding-bottom
l - sets margin-left or padding-left
r - sets margin-right or padding-right
x - sets both padding-left and padding-right or margin-left and margin-right
y - sets both padding-top and padding-bottom or margin-top and margin-bottom
blank - sets a margin or padding on all 4 sides of the element

0 - sets margin or padding to 0
1 - sets margin or padding to .25rem (4px if font-size is 16px)
2 - sets margin or padding to .5rem (8px if font-size is 16px)
3 - sets margin or padding to 1rem (16px if font-size is 16px)
4 - sets margin or padding to 1.5rem (24px if font-size is 16px)
5 - sets margin or padding to 3rem (48px if font-size is 16px)
auto - sets margin to auto

Removing Elements Dynamically

Source: https://www.abeautifulsite.net/adding-and-removing-elements-on-the-fly-using-javascript

  • function removeElement(elementId) {
  • // Removes an element from the document
  • var element = document.getElementById(elementId);
  • element.parentNode.removeChild(element);
  • }

Events through ()

Source: https://blog.bitsrc.io/event-binding-mechanism-in-angular-b38f0e46d2ed


In Angular, we can register and capture an event from an element by wrapping the event in a () parenthesis.
We can bind most of the common events in the DOM:
(focus)="focusCallback()"  
(blur)="blurCallback()"
(submit)="submitCallback()" 
(scroll)="scrollCallback()"
(cut)="cutCallback()"
(copy)="copyCallback()"
(paste)="pasteCallback()"
(keydown)="keydownCallback()"
(keypress)="keypressCallback()"
(keyup)="keyupCallback()"
(mouseenter)="mouseenterCallback()"
(mousedown)="mousedownCallback()"
(mouseup)="mouseupCallback()"
(click)="clickCallback()"
(dblclick)="dblclickCallback()"
(drag)="dragCallback()"
(dragover)="dragoverCallback()"
(drop)="dropCallback()"

How do I copy folder with files to another folder in Unix/Linux?

Souce: https://stackoverflow.com/questions/14922562/how-do-i-copy-folder-with-files-to-another-folder-in-unix-linux

The option you're looking for is -R.
cp -R path_to_source path_to_destination/
  • If destination doesn't exist, it will be created.
  • -R means copy directories recursively. You can also use -r since it's case-insensitive.
  • Note the nuances with adding the trailing / as per @muni764's comment.

Spread syntax

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// expected output: 6

console.log(sum.apply(null, numbers));

// expected output: 6

How to extend an existing JavaScript array with another array, without creating a new array

Source: https://stackoverflow.com/questions/1374126/how-to-extend-an-existing-javascript-array-with-another-array-without-creating

The .push method can take multiple arguments. You can use the spread operator to pass all the elements of the second array as arguments to .push:
>>> a.push(...b)
If your browser does not support ECMAScript 6, you can use .apply instead:
>>> a.push.apply(a, b)
Or perhaps, if you think it's clearer:
>>> Array.prototype.push.apply(a,b)
Please note that all these solutions will fail with a stack overflow error if array b is too long (trouble starts at about 100,000 elements, depending on the browser). If you cannot guarantee that b is short enough, you should use a standard loop-based technique described in the other answer.

Call a “local” function within module.exports from another function in module.exports?

Source: https://stackoverflow.com/questions/10462223/call-a-local-function-within-module-exports-from-another-function-in-module-ex
https://stackoverflow.com/questions/33078967/node-js-call-a-local-function-within-module-exports/33080602

I think I got it. I just changed this.foo() to module.exports.foo() and it seems to be working.
If someone has a better or more proper method, please feel free to correct me.

"use strict"
var self = module.exports = {
    myName : function(req, res, next) {
        // accessing method within controller
        self.getName(data);
    },

    getName : function(data) {
        // code
    }
}

MySQL: Update If exist

Source: https://stackoverflow.com/questions/46628616/ignore-update-data-if-exist-in-mysql-by-node-js

https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html

Fixed it by :
ON DUPLICATE KEY UPDATE

connection.connect( function( err ) {

    connection.query( "INSERT INTO real_user SET ? ON DUPLICATE KEY UPDATE updated_at = '" + data.created_at
     + "', request_count = '" + data.request_count + "'", 
        data,  function( err, res ) {
            connection.end();
        });

    });

node.js sync nested mysql queries

Source: https://www.codexpedia.com/javascript/nodejs-sync-nested-mysql-queries/

Node.js is asynchronous I/O that other processes can start and doesn’t have to wait for some long running input/output processes such read and write from files or databases.For example, you are running five queries in for loop, the result of each loop will be appended to an array. Right after the for loop, you are calling the callback function to get all the results, but the chances are you will get an empty array because the line after the for loop is executed before the lines in the for loop. The code snippet below demonstrates how to make sure all the queries are processed before make the callback function. It declares an counting variable pending and set it to the number of queries to be ran. After a query is executed successfully, decrease the pending by 1, call the callback function if when pending is equal to 0.

var mysql = require('mysql');
var pool =  mysql.createPool({
host : 'localhost',
user : 'root',
password: '',
database: 'test'
});

function getStudents(ids, cb) { 
    var students = [];
    var pending = ids.length;

    for(var i in ids) {
        pool.query('SELECT * FROM students WHERE id = ?', [ ids[i] ], function(err, stu){
            students.push(stu);
            if( 0 === --pending ) {
                cb(students); //callback if all queries are processed
            }
        });
    }
}

var ids = [1,2,3,4,5];
getStudents(ids, function(students){
    console.log(students);

});

The table used in the demonstration above.

CREATE database `test`;
CREATE TABLE `students` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `grade` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `students` (`id`, `name`, `grade`)
VALUES
    (1,'ken',90),
    (2,'jim',90),
    (3,'kay',57),
    (4,'jerry',57),

    (5,'wen',66);

Renewing Facebook Graph API token automatically?

  Mã truy cập dài hạn https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived/ https://community.n8n.io/t/re...