用于日期的计算,功能比较全,我常用的主要是日期的计算(多一天少一天,或者添加几个月等),日期格式化
网址:http://momentjs.cn/
测试代码:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <meta charset="utf-8" /> </head> <body> <script type="text/javascript" src="http://momentjs.cn/downloads/moment.js"></script> <script type="text/javascript"> var d1 = moment('2019-01-29 14:27:00').add(1, 'minutes').format("YYYY-MM-DD HH:mm:ss"); var d2 = moment('2019-01-29 14:27:00').add(1, 'hours').format("YYYY-MM-DD HH:mm:ss"); var d3 = moment('2019-01-29 14:27:00').add(1, 'days').format("YYYY-MM-DD HH:mm:ss"); var d4 = moment('2019-01-29 14:27:00').add(1, 'months').format("YYYY-MM-DD HH:mm:ss"); //var d5 = moment('2019-01-29 14:27:00').add(-1, 'minutes').format("YYYY-MM-DD HH:mm:ss"); //console.log(d5); //改变时间 var changeTime = function (timeType, timeNumber) { var WaringTime = '2019-01-29 14:27:00'; console.log(WaringTime); //var waringDate = new Date(WaringTime); var strStartTime = null; var strEndTime = null; var type = ''; if (timeType == '分钟') { type = 'minutes'; } else if (timeType == '小时') { type = 'hours'; } else if (timeType == '天') { type = 'days'; } else if (timeType == '月') { type = 'months'; } strStartTime = moment(WaringTime).add(-timeNumber, type).format("YYYY-MM-DD HH:mm:ss"); strEndTime = moment(WaringTime).add(timeNumber, type).format("YYYY-MM-DD HH:mm:ss"); console.log(strStartTime); console.log(strEndTime); //$("#StartTime").val(strStartTime); //$("#EndTime").val(strEndTime); } changeTime('月', 1); </script> </body> </html>