1587762960
In any project, the handling of datetime is inevitable, especially some specific datetime projects such as booking, calendar, etc. require thorough application and knowledge enhancement. , how to calculate, parse, validate datetime that language provides. In javscript, moment.js
is a powerful library that supports these complex tasks.
moment.js
support on many browser environments, nodejs, and support many ways to install npm, bower, cdn, …
Here we will install on a javascript project using npm
npm install moment
There are many ways to create a moment object with the current date:
moment();
moment(undefined);
moment([]);
moment({});
moment(new Date());
Note: the
moment(null)
moment object is invalid
Here we want to create a moment object with any specific datetime, there are the following ways:
Because datetime has many types of formats, the way to create the moment above may be inaccurate and not supported in some browsers if the string is invalid. So we should use the moment to create the moment with the string format parameter below
If we know the exact datetime format, we can use the string format to parse
moment("12-25-1995", "MM-DD-YYYY");
moment("2020-10-20 5:30:15", "YYYY-MM-DD HH:mm:ss");
moment({ years:2020, months:10, date:20, hours:15, minutes:10, seconds:3, milliseconds:123});
Note: month has a value from 0 - 11
Create moment object with javascript Date object
const day = new Date(2020, 10, 16);
const dayWrapper = moment(day);
Note:
dayWrapper
Clone from theDate
object, the changes on these two objects are independent, do not affect each other
moment([2020, 10, 14, 15, 25, 50, 125]); // Tạo moment object với parameters [year, month, day, hour, minute, second, millisecond]
Similarly new Date(Number)
, we can create a moment object from the number of seconds / miliseconds from the Unix Epoch timeline (Jan 1 1970 12AM UTC)
moment(1318781876406); // Timestamp miliseconds
moment.unix(1318781876); // Timestamp seconds
UTC is a time with an international standard time zone of 0, which is independent of the locale’s time zone.
By default, a moment object will be parsed and displayed according to local time (for example, VN has a timezone of +7 + GMT + 0700). If we want parse and display moment in UTC then use that moment.utc()
insteadmoment()
moment().format(); // 2020-04-04T10:35:24+07:00
moment.utc().format(); // 2020-04-04T18:35:24+00:00
We can check if a moment object has a valid datetime or not with the method moment().isValid()
Change the moment object by setting the value in the corresponding units
moment().millisecond(Number);
moment().second(Number);
moment().minute(Number);
moment().hour(Number);
moment().date(Number); // date of month 1-31
moment().day(Number|String); // day of week Sunday as 0 and Saturday as 6
moment().month(Number|String); // from 0 to 11
moment().year(Number);
Get the value in the corresponding units
moment().millisecond(); // Number
moment().second(); // Number
moment().minute(); // Number
moment().hour(); // Number
moment().date(); // Number
moment().day(); // Number
moment().month(); // Number
moment().year(); // Number
Find the largest date using the method max
moment.max(Moment[]) // return max moment object
const a = moment().subtract(1, 'day');
const b = moment().add(1, 'day');
moment.max([a, b]); // b
Find the smallest date using the method min
moment.min(Moment[]) // return min moment object
const a = moment().subtract(1, 'day');
const b = moment().add(1, 'day');
moment.min([a, b]); // a
After we create the moment object; We need to perform operations, calculations on the object just created with the following methods
Change the moment object by adding more time or a period of time
moment().add(Number, String);
moment().add(Duration);
moment().add(Object);
moment().add(1, 'days'); // Add 1 day to the current time
moment().add (1, 'days'). add (1, 'months'); // Add 1 day and 1 month to the current time using the chaining method
moment().add ({days: 1, months: 1}); // Add 1 day and 1 month to the current time using the object parameter
Change the moment object by subtracting time or a period of time
moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);
moment().subtract(1, 'days'); // Subtract 1 day from the current time
moment().subtract(1, 'days').subtract(1, 'months'); // Subtract 1 day and 1 month into current time using chaining method
moment().subtract({days: 1, months: 1}); // Subtract 1 day and 1 month from current time using object parameter
Change the moment object by setting it to the starting value of a unit of time (year, month, day, hour, hour, minute, second)
End of time will be similar to Start of Time but with the end value of a time unit
Same for month, day, hour, minute, second
#javascript #momentjs
1606912089
#how to build a simple calculator in javascript #how to create simple calculator using javascript #javascript calculator tutorial #javascript birthday calculator #calculator using javascript and html
1626321063
PixelCrayons: Our JavaScript web development service offers you a feature-packed & dynamic web application that effectively caters to your business challenges and provide you the best RoI. Our JavaScript web development company works on all major frameworks & libraries like Angular, React, Nodejs, Vue.js, to name a few.
With 15+ years of domain expertise, we have successfully delivered 13800+ projects and have successfully garnered 6800+ happy customers with 97%+ client retention rate.
Looking for professional JavaScript web app development services? We provide custom JavaScript development services applying latest version frameworks and libraries to propel businesses to the next level. Our well-defined and manageable JS development processes are balanced between cost, time and quality along with clear communication.
Our JavaScript development companies offers you strict NDA, 100% money back guarantee and agile/DevOps approach.
#javascript development company #javascript development services #javascript web development #javascript development #javascript web development services #javascript web development company
1587762960
In any project, the handling of datetime is inevitable, especially some specific datetime projects such as booking, calendar, etc. require thorough application and knowledge enhancement. , how to calculate, parse, validate datetime that language provides. In javscript, moment.js
is a powerful library that supports these complex tasks.
moment.js
support on many browser environments, nodejs, and support many ways to install npm, bower, cdn, …
Here we will install on a javascript project using npm
npm install moment
There are many ways to create a moment object with the current date:
moment();
moment(undefined);
moment([]);
moment({});
moment(new Date());
Note: the
moment(null)
moment object is invalid
Here we want to create a moment object with any specific datetime, there are the following ways:
Because datetime has many types of formats, the way to create the moment above may be inaccurate and not supported in some browsers if the string is invalid. So we should use the moment to create the moment with the string format parameter below
If we know the exact datetime format, we can use the string format to parse
moment("12-25-1995", "MM-DD-YYYY");
moment("2020-10-20 5:30:15", "YYYY-MM-DD HH:mm:ss");
moment({ years:2020, months:10, date:20, hours:15, minutes:10, seconds:3, milliseconds:123});
Note: month has a value from 0 - 11
Create moment object with javascript Date object
const day = new Date(2020, 10, 16);
const dayWrapper = moment(day);
Note:
dayWrapper
Clone from theDate
object, the changes on these two objects are independent, do not affect each other
moment([2020, 10, 14, 15, 25, 50, 125]); // Tạo moment object với parameters [year, month, day, hour, minute, second, millisecond]
Similarly new Date(Number)
, we can create a moment object from the number of seconds / miliseconds from the Unix Epoch timeline (Jan 1 1970 12AM UTC)
moment(1318781876406); // Timestamp miliseconds
moment.unix(1318781876); // Timestamp seconds
UTC is a time with an international standard time zone of 0, which is independent of the locale’s time zone.
By default, a moment object will be parsed and displayed according to local time (for example, VN has a timezone of +7 + GMT + 0700). If we want parse and display moment in UTC then use that moment.utc()
insteadmoment()
moment().format(); // 2020-04-04T10:35:24+07:00
moment.utc().format(); // 2020-04-04T18:35:24+00:00
We can check if a moment object has a valid datetime or not with the method moment().isValid()
Change the moment object by setting the value in the corresponding units
moment().millisecond(Number);
moment().second(Number);
moment().minute(Number);
moment().hour(Number);
moment().date(Number); // date of month 1-31
moment().day(Number|String); // day of week Sunday as 0 and Saturday as 6
moment().month(Number|String); // from 0 to 11
moment().year(Number);
Get the value in the corresponding units
moment().millisecond(); // Number
moment().second(); // Number
moment().minute(); // Number
moment().hour(); // Number
moment().date(); // Number
moment().day(); // Number
moment().month(); // Number
moment().year(); // Number
Find the largest date using the method max
moment.max(Moment[]) // return max moment object
const a = moment().subtract(1, 'day');
const b = moment().add(1, 'day');
moment.max([a, b]); // b
Find the smallest date using the method min
moment.min(Moment[]) // return min moment object
const a = moment().subtract(1, 'day');
const b = moment().add(1, 'day');
moment.min([a, b]); // a
After we create the moment object; We need to perform operations, calculations on the object just created with the following methods
Change the moment object by adding more time or a period of time
moment().add(Number, String);
moment().add(Duration);
moment().add(Object);
moment().add(1, 'days'); // Add 1 day to the current time
moment().add (1, 'days'). add (1, 'months'); // Add 1 day and 1 month to the current time using the chaining method
moment().add ({days: 1, months: 1}); // Add 1 day and 1 month to the current time using the object parameter
Change the moment object by subtracting time or a period of time
moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);
moment().subtract(1, 'days'); // Subtract 1 day from the current time
moment().subtract(1, 'days').subtract(1, 'months'); // Subtract 1 day and 1 month into current time using chaining method
moment().subtract({days: 1, months: 1}); // Subtract 1 day and 1 month from current time using object parameter
Change the moment object by setting it to the starting value of a unit of time (year, month, day, hour, hour, minute, second)
End of time will be similar to Start of Time but with the end value of a time unit
Same for month, day, hour, minute, second
#javascript #momentjs
1593507275
These functions are very important for every JavaScript Developer and are used in almost every JavaScript Library or Framework. Check out the code snippet below.
Taken from the very popular library Lodash
/**
* Creates a function that invokes `func` with arguments reversed.
*
* @since 4.0.0
* @category Function
* @param {Function} func The function to flip arguments for.
* @returns {Function} Returns the new flipped function.
* @see reverse
* @example
*
* const flipped = flip((...args) => args)
*
* flipped('a', 'b', 'c', 'd')
* // => ['d', 'c', 'b', 'a']
*/
function flip(func) {
if (typeof func !== 'function') {
throw new TypeError('Expected a function')
}
return function(...args) {
return func.apply(this, args.reverse())
}
}
export default flip
Look at the statement on line 21, return func.apply(this, args.reverse())
In this article, we will have a look at the call(), apply() and bind() methods of JavaScript. Basically these 3 methods are used to control the invocation of the function. The call() and apply() were introduced in ECMAScript 3 while bind() was added as a part of ECMAScript 5.
Let us start with an example to understand these.
Suppose you are a student of X university and your professor has asked you to create a math library, for an assignment, which calculates the area of a circle.
const calcArea = {
pi: 3.14,
area: function(r) {
return this.pi * r * r;
}
}
calcArea.area(4); // prints 50.24
You test this and verify its result, it is working fine and you upload the library to portal way before the deadline ends. Then you ask your classmates to test and verify as well, you come to know that that your result and their results mismatches the decimals precision. You check the assignment guidelines, Oh no! The professor asked you to use a constant **pi** with 5 decimals precision. But you used **3.14** and not **3.14159** as the value of pi. Now you cannot re-upload the library as the deadline date was already over. In this situation, **call()** function will save you.
#js #javascript-development #javascript #javascript-interview #javascript-tips
1622207074
Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.
JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.
JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.
Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.
In JavaScript, ‘document.write‘ is used to represent a string on a browser.
<script type="text/javascript">
document.write("Hello World!");
</script>
<script type="text/javascript">
//single line comment
/* document.write("Hello"); */
</script>
#javascript #javascript code #javascript hello world #what is javascript #who invented javascript