程序猿
讲互联网的故事

Javascript(js)动态获取当前日期、时间、星期的代码片段

直接复制代码片段,粘贴到要显示的html结构中(<span>粘贴到此处</span>)

获取格式:年、月、日

    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    var day = date.getDate();    
    document.write("今天是"+year+"年"+month+"月"+day+"日");

 

获取格式:年、月、日、时、分

    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    var day = date.getDate();
    var hours = date.getHours();
    var min = date.getMinutes();
    document.write("今天是"+year+"年"+month+"月"+day+"日"+hours+":"+min);

 

获取格式:年、月、日、星期

    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    var day = date.getDate();
    var xingqi = date.getDay();
    switch(xingqi){
        case 0:
            xingqi = "日";
            break;
        case 1:
            xingqi = "一";
            break;
        case 2:
            xingqi = "二";
            break;
        case 3:
            xingqi = "三";
            break;
        case 4:
            xingqi = "四";
            break;
        case 5:
            xingqi = "五";
            break;
        case 6:
            xingqi = "六";
            break;    
    }

    document.write("今天是"+year+"年"+month+"月"+day+"日"+"星期"+xingqi);
赞(0) 打赏一下

评论 抢沙发

觉得文章有用就打赏一下博客作者

支付宝扫一扫打赏

微信扫一扫打赏