Data built in objects

1. Built in object Date Date object

2. Create date object

2.1 create a date object based on the current system time.

    var date1 = new Date();
    //a. Output date object information
    console.log(date1); //Thu Dec 14 2017 20:55:24 GMT+0800 
    console.log(date1.toString()); //Thu Dec 14 2017 20:55:24 GMT+0800 
    //b. Output the date part of the date object.
    console.log(date1.toDateString()); //Thu Dec 14 2017
    console.log(date1.toLocaleDateString()); //2017/12/14
    //c. Output time part in Date object
    console.log(date1.toTimeString());     // 20:55:24 GMT+0800 (China standard time)
    console.log(date1.toLocaleTimeString()); // 8:55:24 PM

2.2 create a date object based on the specified system time. There are three formats for parameters

    //a. Comma separated values of type number
    var date2 = new Date(2000,5,6,8,20,30); //The month starts at 0.
    console.log(date2); //Tue Jun 06 2000 08:20:30 GMT+0800 (China standard time)
    //b. Date in string
    
    var date3 = new Date("2000-5-6  8:20:30");
    console.log(date3); //Sat May 06 2000 08:20:30 GMT+0800 (China standard time)
    //c. Give millisecond form
    var date4 = new Date(1000); //1 second = 1000 milliseconds
    console.log(date4);  //The milliseconds given start at 8 a.m. on January 1, 1970. Thu Jan 01 1970 08:00:01 GMT+0800 (China standard time)

2.3 get the current time in milliseconds

    var date5 = Date.now();
    console.log(date5); // 1513256404915

2.4 get some contents of date object.

    // getTime() returns the same number of milliseconds as valueOf()
    // getMilliseconds()
    // getSeconds() returns 0-59
    // getMinutes() returns 0-59
    // getHours() returns 0-23
    // getDay() returns the day of the week 0 Sunday 6 week 6
    // getDate() returns the day of the current month, the day of the current month
    // getMonth() returns the month, starting from 0
    // getFullYear() returns a 4-digit year, such as 2016
    // console.log('--------------------------------');
    var date = new Date();
    console.log(date.getFullYear());
    console.log(date.getDate());
    console.log(date.getDay());
    console.log(date.getDate());
    console.log(date.getMonth());
    console.log(date.getFullYear());

2.5 write a function to pass a date object and return a string in the form of yyyy MM DD HH: mm: SS

In fact, it can be used moment.js

Method 1:

    function getStrOfDate(date){
        var year = date.getFullYear();
        var month = date.getMonth()+1;
        month = month < 10? "0"+month : month;
        var day = date.getDate();
        var hour = date.getHours();
        var min = date.getMinutes();
        var sec = date.getSeconds();
        return  year+"-"+month+"-"+day+" "+hour+":"+min+":"+sec;
    }
    var date1 = new Date();
    console.log(getStrOfDate(date1));// 2017-12-14 21:3:49

Method 2:

    function getDate(a){
        return a.getFullYear()+"-"+a.getMonth()+"-"+a.getDate()+" "+a.getHours()+":"+a.getMinutes()+":"+a.getSeconds();
    }
    var date11 = new Date();
    console.log(getDate(date11)); //2017-12-14 21:3:49

Tags: Javascript

Posted on Mon, 25 May 2020 12:00:41 -0400 by skypilot