怎么把js获取的系统时间加一年

怎么把js获取的系统时间加一年,第1张

将年+1即可,代码如下:

var now = new Date()

now.setFullYear(now.getFullYear()+1)

alert(now)

给你趴一个看看,先把时间戳转为时间,然后+1年,然后在转为时间戳

(function($) {

$.extend({

myTime: {

/**

* 当前时间戳

* @return <int> unix时间戳(秒)

*/

CurTime: function(){

return Date.parse(new Date())/1000

},

/**

* 日期 转换为 Unix时间戳

* @param <string>2014-01-01 20:20:20 日期格式

* @return <int> unix时间戳(秒)

*/

DateToUnix: function(string) {

var f = string.split(' ', 2)

var d = (f[0] ? f[0] : '').split('-', 3)

var t = (f[1] ? f[1] : '').split(':', 3)

return (new Date(

parseInt(d[0], 10) || null,

(parseInt(d[1], 10) || 1) - 1,

parseInt(d[2], 10) || null,

parseInt(t[0], 10) || null,

parseInt(t[1], 10) || null,

parseInt(t[2], 10) || null

)).getTime() / 1000

},

/**

* 时间戳转换日期

* @param <int>unixTime待时间戳(秒)

* @param <bool>isFull返回完整时间(Y-m-d 或者 Y-m-d H:i:s)

* @param <int> timeZone 时区

*/

UnixToDate: function(unixTime, isFull, timeZone) {

if (typeof (timeZone) == 'number')

{

unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60

}

var time = new Date(unixTime * 1000)

var ymdhis = ""

ymdhis += time.getUTCFullYear() + "-"

ymdhis += (time.getUTCMonth()+1) + "-"

ymdhis += time.getUTCDate()

if (isFull === true)

{

ymdhis += " " + time.getUTCHours() + ":"

ymdhis += time.getUTCMinutes() + ":"

ymdhis += time.getUTCSeconds()

}

return ymdhis

}

}

})

})(jQuery)

调用边的

<script>

document.write($.myTime.DateToUnix('2016-04-12 10:49:59')+'<br>')

document.write($.myTime.UnixToDate(1460429399))

</script>

需要准备的材料分别有:电脑、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天的日期结果。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11855918.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存