JavaScript之获得日期函数

1.获得当前日期

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>日期函数</title>
</head>
<body>
    
</body>
</html>
<script type="text/javascript">
    //创建日期方式一
    //普及:标准时间都是从1970年1月1日0时0分0秒开始计算
    //new Date:获得系统的当前时间,是根据格林尼治时间划分
    var date = new Date();
    console.log(date);

    //创建日期方式二
    //从1970年开始往后加上100毫秒得到的日期,又因为时区的不同,故得到的会不同
    var date1 = new Date(100);
    console.log(date1);    

    //创建日期方式三
    //月份是0~11
    var date2 = new Date(2019,5,1);
    console.log(date2);
</script>


2.获得特定时间

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>得到日期</title>
</head>
<body>
    
</body>
</html>
<script type="text/javascript">
    var date = new Date(2019,5,1,23,30,30);
    //获得年
    var year = date.getFullYear();
    console.log(year);
    //获得月
    var month = date.getMonth();
    console.log(month);
    //获得具体日期几号
    var mydate = date.getDate();
    console.log(mydate);
    //获得星期几
    var day = date.getDay();
    console.log(day);
    //获得小时
    var hour = date.getHours();
    console.log(hour);
    //获得1970到现在的时间间隔毫秒数
    var million = date.getTime();
    console.log(million);
</script>

猜你喜欢

转载自www.cnblogs.com/zjm1999/p/10841771.html