+
95
-

js如何生成一年中的随机日期?

js如何生成一年中的随机日期?比如9月到12月之间?

网友回复

+
15
-

思路就是时间段之间的时间差乘以随机就可以了,代码如下:

<script type="text/javascript">
        //时间戳转换成时间
        function timestampTotimestr(timestamp) {
            var date = new Date(timestamp);
            Y = date.getFullYear() + '-';
            M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1): date.getMonth()+1) + '-';
            D = date.getDate() + ' ';
            h = (date.getHours() < 10 ? '0' + date.getHours(): date.getHours()) + ':';
    ...

点击查看剩余70%

我知道答案,我要回答