JavaScript date function

Tram Ho

The function handles dates (Date) in Javascript

  • Previous article we have learned about the Date object in Javascript, then in this article I will talk about some of the processing functions related to this Date object. This article is quite important when you write applications that are related to date and time, for example, the clock display application, showing the count down time in discount deals.

We are divided into two main groups of functions: the Date Get group and the Date Set group.

Table of Content

    1. Date Get group functions in Javascript
    1. Date Set groups in Javascript
    1. Create an online clock using Javascript
    • CheckTime ()
    • StartTime ()
  1. Epilogue

1. Date Get group functions in Javascript

In JavaScript, there are 10 common time setting functions:


  • getDate () get dates (1 – 31)
  • getDay () get the day of the week (0-6)
  • getFullYear () get the full year (YYYY)
  • getYear () takes the last 2 years (YY)
  • getHours () get hours (0 – 23)
  • getMiliSeconds () get the number of milliseconds (0 – 999)
  • getMinutes () get minutes (0 – 59)
  • getMonth () get months (0 – 11)
  • getSeconds () get the number of seconds (0 – 59)
  • getTime () time has been converted to miliseconds. **

The use of the above functions is quite simple, because it is the method of the Date object, so you just need to call and use.

Note : with the function of taking the day of the week getDay (), you must add one to get the correct result because it is calculated from 0.

2. Date Set groups in Javascript

Corresponding to each Date Get function, there will be a Date Set function (except getDay ()).

  • setDate () set the date (1 – 31)
  • setFullYear () set the full year (YYYY)
  • setYear () set the last 2 digits (YY)
  • setHours () sets the number of hours (0 – 23)
  • setMiliSeconds () sets the number of milliseconds (0 – 999)
  • setMinutes () set the number of minutes (0 – 59)
  • setMonth () set the month (0 – 11)
  • setSeconds () sets the number of seconds (0 – 59)
  • setTime () sets the time that has been converted to miliseconds.

Note:

  • Because this is a set function, you must pass parameters.
  • The functions affect each other, for example, if you set the date and time incorrectly, it will take the default date and time.
  • If you use the setTime () function to set it will affect all the remaining values ​​because setTime () is the full time setting function converted to the miniseconds form.

3. Create an online clock using Javascript

In order for you to better understand the application of the functions in the Date object, I will make an example of displaying an online clock, this is a clock taken from the time of the Client computer system.

First you need DEMO to make it easier to visualize, after watching it, we proceed to do it.

You create an index.html file with the following content:

Including:

  • A div # result tag is used to display the clock
  • The body tag has the onload = “startTime ()” event to run the clock when the website is loaded.
  • There is a startTime () function for creating clocks and a checkTime () function for converting numbers into 01, 02, 03, etc.

CheckTime ()

StartTime ()

  • With this method, you only get the time of the Client system, but if you want to get time from the server, you have to go through PHP, and use the setTimeout () function to increase hours, minutes, seconds, not directly. continue like this.

4. Conclusion

  • In addition to the above functions, there are still many other functions but I cannot list them all, so you can refer here
  • To use these functions well, you have to do a few more examples but since this is a series written on theory, I will not give more.
Share the news now

Source : Viblo