SoFunction
Updated on 2025-04-07

Summary of Date usage in JS

Date() date object

Date() Date object is a constructor. We must use new to create our date object.

// 1. Use Date if there is no parameter Return the current time of the current system	var date = new Date();
	(date);
// 2. Commonly used writing methods for parameters Number type 2019, 10, 01 or string type '2019-10-1 08:08:08'	var date1 = new Date(2019, 10, 1);  
	(date1);  //The return is November not October	var date2 = new Date('2019-10-1 8:8:8');  //The most commonly used	(date);
  • The Date object is different from the Math object. It is a constructor, so we need to instantiate it before using it.
  • Date instances are used to process dates and times

Use of Date() method

1. Get the current time must be instantiated

var now = new Date();
	(now);

()Constructor parameters

If there is time in the brackets, the time in the parameter is returned. For example, the date format string is ‘2019-5-1’, which can be written as new Date(‘2019-5-1’) or new Date(‘2019/5/1’)

Date formatting

We want dates in 2019-9-8 8:8:8 format, what should we do?

You need to get the part specified by the date, so we need to get this method manually.

Method name illustrate Code
getFullYear() Obtain the year ()
getMonth() Get the month (0-11) ()
getDate() Get the date of the day ()
getDay() Get the day of the week (Sunday 0 to Saturday 6) ()
getHours() Get the current hour ()
getMinutes() Get the current minute ()
getSconds() Get the current second ()
// Let's write a Wednesday, May 1, 2019	var year = ();
	var month = () + 1;
	var dates = ();
	var arr = ['Sunday', 'Monday', 'Tuesday','Wednesday', 'Thursday', 'Friday', 'Saturday'];
	var day = ();
	('Today is:' + year + 'Year' + month + 'moon' + dates + 'day' + arr[day]);

Date formatting hours, minutes and seconds

// Encapsulate a function to return the current time, minute and second format 08:08:08	function getTime() {
        var times = new Date();
        var h = ();
        h = h < 10 ? '0' + h : h;
        var m = ();
        m = m < 10 ? '0'+ m : m;
        var s = ();
        s = s < 10 ? '0'+ s : s;
        return h + ':' + m + ':' + s;
    }
	(getTime());

Get the total millisecond form of date

The Date object is based on the number of milliseconds from January 1, 1970 (UNF)

We often use the total number of milliseconds to calculate time because it is more accurate

// Get the total number of milliseconds of Date (timestamp) not the number of milliseconds of the current time, but how many milliseconds have passed since January 1, 1970// 1. Pass valueOf() getTime()	var date = new Date;
	(());
	(());
// 2. Simple writing method (the most commonly used writing method)	var date1 = +new Date();  // +new Date() returns the total number of milliseconds// 3.H5 Newly added Get the total number of milliseconds	(());

Countdown case

// Core algorithm: The input time subtracts the current time is the remaining time, that is, the countdown, but you cannot look at the time, minute and second subtraction// Use a timestamp to do it. The user enters the total number of milliseconds of time minus the total number of milliseconds of current time, and what he gets is the number of milliseconds of remaining time// Convert the total number of milliseconds of the remaining time to days, hours, minutes, and seconds (convert the timestamp to time, minutes and seconds)// The conversion formula is as follows:/*
     d = parseInt(total seconds/60/60/24); // Calculate the number of days
     h = parseInt(total seconds/60/60%24); // Calculate hours
     m = parseInt(total seconds/60/%60); // Calculate the fraction
     s = parseInt(total seconds %60); // Calculate the current number of seconds
 */
  function conutDown(time) {
    var nowTime = +new Date() // Returns the total number of milliseconds of the current time    var inputTime = + new Date(time); // Returns the total number of milliseconds of the user input time    var times = (inputTime - nowTime) / 1000; // time is the total number of seconds remaining    var d = parseInt(times / 60 / 60 / 24);  // sky    d = d < 10 ? 0 + d : d;
    var h = parseInt(times / 60 / 60 % 24);     // hour    h = h < 10 ? 0 + h : h;
    var m = parseInt(times / 60 % 60);       // point    m = m < 10 ? 0 + m : m;
    var s = parseInt(times % 60);            // Current seconds    s = s < 10 ? 0 + s : s;
    return d + 'sky' + h + 'hour' + m + 'point' + s + 'Second';
  }
  var date = new Date();
  (conutDown('2022-10-1 8:8:8'));

This is the end of this article about the use of Date() of JS. For more information about the use of Date() of JS, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!