js如何得到当前时间再加几天并且让时间自动变成下一年或者下一个月 例如: 2012-12-11加上30天 2013-1-10

JavaScript010

js如何得到当前时间再加几天并且让时间自动变成下一年或者下一个月 例如: 2012-12-11加上30天 2013-1-10,第1张

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<script>标签,输入js代码:

var a = '2012-12-11'

var date1 = new Date(a.replace('-', '/'))

var t = date1.getTime() + 30 * 24 * 3600 * 1000

var date2 = new Date(t)

document.body.innerText = date2

3、浏览器运行index.html页面,此时打印出了2012-12-11加上30天的日期结果。

<html>

<script type="text/javascript">

     

    function showNext(){

        currentTime = document.getElementById("currentTime")     

         nextTime = document.getElementById("nextTime")

        if(currentTime.value==""){

            alert("please input current time!")

            return    

        }

        var currentTimeLength = currentTime.value.length

        switch(currentTimeLength){

            case 4:

                inputText(goto4())

            break    

            case 7:

                inputText(goto7())

                break

            default:

            break

        }

    }

    function goto4(){

            var NumberOfValue = parseInt(currentTime.value)

            return NumberOfValue+1

        }

    function goto7(){

            var numberOfValue = currentTime.value.split("-")

            var month = parseInt(numberOfValue[1])

            if(month==12){

                var year = parseInt(numberOfValue[0])

                return year+1+"-0"+1    

            }else{

                return numberOfValue[0]+"-0"+(month+1)    

            }

        }

    function inputText(obj){

        nextTime.value = obj    

    }

    

</script>

<body>

please input current time :<input type="text"  id="currentTime"/></br>

<input type="button" value ="click to show next time" onclick="showNext()"/></br>

the next time is:<input type="text" id="nextTime"/>

</body>

</html>

刚写的测试通过。你试试。