Date object in ...LOGIN

Date object in JavaSCript

Date object

  • Date object is different from String object. Defining a string is actually a String object. You can call properties and methods directly.

  • To use a Date object, you must use the new keyword to create it. Otherwise, the properties and methods of the Date object cannot be called.


Method to create a Date object

(1) Create an instance of the current (now) date object without any parameters

var today = new Date();

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            //创建现在的日期时间对象实例
            //today就是刚创建的Date对象实例
            var today=new Date();
            document.write(today);
        </script>
    </head>
    <body>
    </body>
</html>

(2) Create a date object instance with the specified timestamp, and the parameter is the timestamp.

Time stamp: refers to the number of milliseconds that have passed since a certain time at 0:00:00 on January 1, 1970 (1 second = 1000 milliseconds).

var timer = new Date(10000); //The time is 0:00:10 on January 1, 1970

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            //指定毫秒值
            var time=new Date(20000);
            document.write(time);
        </script>
    </head>
    <body>
    </body>
</html>

(3) Specify the date of a string Time information, the parameter is a date and time string

var timer = new Date(“2016/11/11 10:00:00”);

Example: Calculate your How old are you today?

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            //计算你今年多大了
            //1.创建现在的日期对象,取出当前的毫秒值
            var now = new Date();
            var nowTime = now.getTime();
            //2.创建出生日期时的日期对象,取出那时的毫秒值
            var ago = new Date("1992/10/10");
            var agoTime = ago.getTime();
            //3.两个毫秒相减
            var str=(nowTime-agoTime)/1000/3600/24/365;
            document.write("你今年"+str+"岁了")
        </script>
    </head>
    <body>
    </body>
</html>

(4) Specify multiple numerical parameters

var timer = new Date(2015+100,4,25,10,20,0); // The order is: year, month, day, hour, minute, second. Year, month, and day are required.

Example: Calculate how many days you have to live until you are 100 years old.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            //计算你今年多大了
            //1.创建现在的日期对象,取出当前的毫秒值
            var now = new Date();
            var nowTime = now.getTime();
            //2.创建未来日期时的日期对象,取出那时的毫秒值
            var future = new Date(1992+100,10,10);
            var futureTime = future.getTime();
            //3.两个毫秒相减
            var str=(futureTime-nowTime)/1000/3600/24;
            document.write("你还要活"+str+"天,才能到100岁");
        </script>
    </head>
    <body>
    </body>
</html>
  • getFullYear(): Get the four-digit year.

  • getMonth(): Get the month, value 0-11.

  • getDate(): Get the number, value 1-31

  • ##getHours(): Get the hours.

  • getMinutes(): Number of minutes

  • getSeconds(): Number of seconds

  • getMilliseconds( ) milliseconds

  • getDay()week

  • getTime() millisecond value, the millisecond value from January 1, 1970 to the present


Next Section

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //创建现在的日期时间对象实例 //today就是刚创建的Date对象实例 var today=new Date(); document.write(today); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware