javascrip的日期

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //Date()  日期函数 是一个构造函数new 来调用创建我们的日期函数
        var d = new Date();
        //avaScript 日期输出
        // 默认情况下,JavaScript 将使用浏览器的时区并将日期显示为全文本字符串:
        console.log(d);

        var a=new Date(2019,10,1,11,10,33,33)
        console.log(a);  //2019.10.1 11:10:33:33

        //字符串的格式
        var datar=new Date('2022-10-01 10:10:8');
        console.log(datar);

        //格式化日期 年日期
        var date=new Date('2022-10-30 10:10:8');
        console.log(date.getFullYear());  //获取当前的日期年 2022 9
        console.log(date.getMonth()+1); //月份  放回月份小一个月   要加1
        console.log(date.getDate()); //返回的是 几号   29
        console.log(date.getDay()); //1-6 是周一到周6  周日放回的是 0  


        //格式化日期 时间、
        var mko=new Date();
        console.log(mko.getHours());//
        console.log(mko.getMinutes());//
        console.log(mko.getSeconds());//



        function nsbhfd(){
            var sjkdfn=new Date
            var mko=sjkdfn.getHours();
            var vy=sjkdfn.getMinutes();
            var nj=sjkdfn.getSeconds();
            pp=nj<10? '0'+nj: nj
            return mko+':'+vy+":"+pp
        }
        console.log(nsbhfd());  //16:51:01


        //定时器
        setInterval(function(){
            document.write(new Date());
        },1000000)
        //每个一秒执行一次

         
        //时间戳
        var sdf=date.getTime(); //当前的时间
        console.log(sdf);

        //简单的写法  获取时间戳
        var date3=+new Date();
        console.log(date3);

 
       //倒计时 
       function name1() {
           var s=new Date().getTime();  //获取当前的时间戳
           var d= +new  Date('2002-12-14 10:0:0'); //用户输入的时间戳

           var pppp=(d-s)/1000 //用户的时间戳 删除  当前的时间戳处1000

           var time=parseInt(pppp/60/60/24)//天

           var time1=parseInt(pppp/60/60%24)//

           var time2=parseInt(pppp/60%60)//分钟
      
         
           var time3=parseInt(pppp%60)//
           var bhu=time3<10? '0'+time3 : time3

           return time+'天'+time1+'小时'+time2+"分钟"+bhu+'秒'
        
       }


    //   每1秒执行一次
       setInterval (function(){
            document.write(name1()+'<br>');
       },1000)
       

 

posted @ 2022-10-10 14:05  python,菜鸟  阅读(12)  评论(0编辑  收藏  举报