js 关于时间的获取

(1)获取当前时间:var myDate=new Date();
(2)获取当前天的日期:var today = myDate.getDate();
(3)获取当前月:var mymonth = myDate.getMonth()+1;
(4)获取当前年:var myyear= myDate.getFullYear();
(5)获取当前小时:var myhour= newDate.getHours();
(6)获取当前分钟:var mymin=newDate.getMinutes();
(7)获取下一小时:var nexthour= new Date(myDate.getTime() +1 * 60 * 60 * 1000).getHours();
(8)获取当前时间是本周的第几天:
var week= myDate.getDay(); //返回值是 0(周日) 到 6(周六) 之间的一个整数
(9)判断是否是闰年:

function IsLeapYear(year) {
	 return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}

(10)获取当前月最大天数

function maxDay() {
  return new Date(myDate.getFullYear(), myDate.getMonth() + 1, 0).getDate();
}

(11)获取上月最大天数

function prevMaxDay() {
  return new Date(myDate.getFullYear(), myDate.getMonth(), 0).getDate();
}

(12)获取本月的第一天

var myMonth=myDate.getMonth()+1;
var currentFirstDay=myDate.getFullYear()+"-"+myMonth+"-"+"1";

(13)获取本月的最后一天

var myMonth=myDate.getMonth()+1;
var day = new Date(year,month,0);      
var lastdate =myDate.getFullYear()+"-"+myMonth+ '-' + day.getDate();

(14)获取昨天的日期:var yesterday= new Date(new Date()-24*60*60*1000);

猜你喜欢

转载自blog.csdn.net/Bambi12/article/details/82964521