Javascript Date Method
Hello, how are you all friends, this time we will continue the discussion of JavaScript programming.
This time we will discuss the date method in javascript. In javascript there are several built-in classes, such as Date, Object, Array, Math, and String. To manipulate related array data, math commands. character manipulation, and object manipulation.
You can study other tutorials.
Javascript Tutorial: Convert time am pm to 24 Hours
Making Loading When Loading Pages
I will give an example using the date method:
const myDate = new Date();
const myDate = new Date(dateString);
const myDate = new Date(miliseconds);
const myDate = new Date(year,month,date,hour,minute,second,millisecond);
I attach some commonly used methods.
In addition, there are also static methods that can be used without the need for instantiation, namely:
Date String Format
when we use date and time, we need to understand the format used by world standards. This is useful and makes it easier for us to convert and manipulate a date. The date string format itself, generally consists of:
From the format table above, when for example we are going to parse either from a string to milliseconds or vice versa, we can take advantage of the above format.
For JavaScript’s own Date Object, the epoch value starts from 0 for January 1, 1970, 00:00:00 UTC
An example of using the date object.
// parameter birthday dapat berupa miliseconds ataupun date string
const myAge = birthday => {
const birtday = new Date(birthday);
const today = Date.now(); // today menghasilkan nilai miliseconds saat ini
const diff_ms = today - birtday.getTime(); // menghitung selisih nilai miliseconds hari ini dan tanggal lahir
const diffDate = new Date(diff_ms);
return diffDate.getFullYear() - 1970; // 1970 adalah representasi 0 dari miliseconds
};
console.log(myAge('2000-01-22')); // 21 tahun
So hopefully this tutorial can be helpful for javascript programmers.
Thanks.