标签:oda int 获取 second trend string 拼接 世界 --
1 function formatDate(datetime) { 2 // 获取年月日时分秒值 slice(-2)过滤掉大于10日期前面的0 3 var year = datetime.getFullYear(), 4 month = ("0" + (datetime.getMonth() + 1)).slice(-2), 5 date = ("0" + datetime.getDate()).slice(-2), 6 hour = ("0" + datetime.getHours()).slice(-2), 7 minute = ("0" + datetime.getMinutes()).slice(-2), 8 second = ("0" + datetime.getSeconds()).slice(-2); 9 // 拼接 10 var result = year + "-"+ month +"-"+ date +" "+ hour +":"+ minute +":" + second; 11 // 返回 12 return result; 13 } 14 15 var date = new Date(); 16 console.log(formatDate(date)); // 2018-05-26 23:09:26
根据开始日期和期限,计算结束日期
1 //date: 日期字符串yyyy-MM-dd,如:2016-02-14 2 //years:年份,正整数字符串 3 //返回日期字符串yyyy-MM-dd,如:2016-02-14 4 function dateAddYear(date, years) { 5 var now = new Date(date); 6 var intYear = now.getFullYear() + parseInt(years); 7 var intMonth = now.getMonth() + 1; //正常的月份, 8 var intDay = now.getDate() - 1; //日期-1 9 if (intDay == 0) { 10 intMonth--; //减少一个月 11 if (intMonth == 0) { 12 intYear--; //0:减少一年 13 intMonth = 12; 14 intDay = 31; 15 } 16 else if (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) { 17 intDay = 30; //4,6,9,11:30天 18 } 19 else if (intMonth == 2) { 20 intDay = 28; //2:28/29 21 if (intYear % 4 == 0) { 22 intDay = 29; 23 } 24 } else { 25 intDay = 31; //1,3,5,7,8,10,12 :31天 26 } 27 } 28 29 var strMonth = (intMonth) < 10 ? "0" + (intMonth).toString() : (intMonth).toString(); 30 var strDay = (intDay) < 10 ? "0" + (intDay).toString() : (intDay).toString(); 31 var strEndDate = intYear + "-" + strMonth + "-" + strDay; 32 return strEndDate; 33 } 34 35 console.log(dateAddYear(‘2018-6-10‘,‘2‘)); // 2020-06-09
beginDate是开始日期,字符串格式
count是指多少天,整型数
注意:setDate和getDate结合使用
date.setDate(date.getDate() + count);
1 function calculateDate(beginDate,count){ 2 var date = new Date(beginDate); 3 date.setDate(date.getDate() + count); 4 var year = date.getFullYear(); 5 var month = ("0" + (date.getMonth()+1)).slice(-2); 6 var day = ("0" + date.getDate()).slice(-2); 7 var hours = ("0" + date.getHours()).slice(-2); 8 var minute = ("0" + date.getMinutes()).slice(-2); 9 var second = ("0" + date.getSeconds()).slice(-2); 10 11 var endDate = year + "-"+ month +"-"+ day +" "+ hours +":"+ minute +":" + second; 12 13 return endDate; 14 } 15 16 console.log(calculateDate(‘2018-5-26 23:50:32‘,30)); // 2018-06-25 23:50:32
标签:oda int 获取 second trend string 拼接 世界 --
原文地址:https://www.cnblogs.com/le220/p/9094838.html