Js Date

本文介绍了一个JavaScript脚本,用于获取当前日期时间,并演示了如何格式化显示这些信息,包括完整日期格式、具体到小时分钟秒的格式以及带有上午下午标识的时间格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 <script type="text/javascript">
        // var thedate = new Date(2013, 02, 06);
       // var thedate = new Date('2013,March, 06');
       // alert(thedate.toLocaleDateString())

 //1.2013-4-9 11:21:32
        var today = new Date();
        //2013年4月9日 星期二
        // alert(today.toLocaleDateString());
        //2013年4月9日
       //alert(today.toLocaleTimeString());

        var year = today.getFullYear();
       var month = today.getMonth();
       var date = today.getDate();
       alert(year + '-' + month + '-' + date + "  " + today.toLocaleTimeString());
        
 //2.2013年4月9日 11点21分32秒
       var hours = today.getHours();
       var minute = today.getMinutes();
       var Seconds = today.getSeconds();
       alert(today.toLocaleTimeString() + "  " + hours + '点' + minute + '分' + Seconds + '秒');
 //3.2013年4月9日 上午11点21分32秒
 //4.2013年4月9日 下午13点21分32秒
        if(hours<12) {
            alert(today.toLocaleTimeString() + "  " +"上午"+ hours + '点' + minute + '分' + Seconds + '秒');
        }
        else{
            alert(today.toLocaleTimeString() + "  " + "下午" + hours + '点' + minute + '分' + Seconds + '秒');
        }
        
 // 5.2011年6月23日 今天我已经毕业N年了 我已经毕业N月了 我已经毕业N天了
//        var thedate = new Date(2011,05,23);
//        var today = new Date();
//        alert(thedate.getTime());
        //        alert(today.getTime());
//        var milseconds = today - thedate;
//        alert('我们毕业' + Math.floor(milseconds / 1000 / 3600 / 24 / 365) + '年多了');

       // alert('我们毕业' + (today.getFullYear() - thedate.getFullYear()) + '年了');
       // alert('我们毕业' + today.getFullYear() - thedate.getFullYear()) * 12 + (today.getMonth() - thedate.getMonth() + '月了');
       

    </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值