1、js获取当前日期
var myDate = new Date();
var year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
var day = myDate.getDate(); //获取当前日(1-31)
var stryear = year.toString();
var strmonth;
var strday;
if (month <= 9) {
strmonth = "0" + month;
}2、日期加一天,原理转换为增加一天的毫秒,然后再转换为时间格式。
function AddDays(date, days) {
var nd = date;
nd = nd.valueOf();
nd = nd + days * 24 * 60 * 60 * 1000;
nd = new Date(nd);
var y = nd.getFullYear();
var m = nd.getMonth() + 1;
var d = nd.getDate();
if (m <= 9) m = "0" + m;
if (d <= 9) d = "0" + d;
var cdate = y + "-" + m + "-" + d;
return cdate;
}3、每月多少天,原理:Date对象月份从0开始,即0表示1月份,以此类推。 在上面的代码中,我们初始化d为三月份的第0天,由于
JavaScript中day的范围为1~31中的值,所以当设为0时,会向前 一天,也即表示上个月的最后一天。
function mGetDate(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var d = new Date(year, month, 0);
return d.getDate();
}地址:https://www.cnblogs.com/sheqiuluo/p/6890300.html
本文介绍JavaScript中关于日期的基本操作方法,包括获取当前日期、日期加一天及计算每月天数等功能的实现。通过简单的函数实现这些功能,便于理解和使用。

569

被折叠的 条评论
为什么被折叠?



