其他分享
首页 > 其他分享> > JS进行时间获取

JS进行时间获取

作者:互联网

一、js获取当前月份的第一天

function getCurrentMonthFirst(){
    var date = new Date();
    date.setDate(1);
    var month = parseInt(date.getMonth()+1);
    var day = date.getDate();
    if (month < 10) {
        month = '0' + month
    }
    if (day < 10) {
        day = '0' + day
    }
    return date.getFullYear() + '-' + month + '-' + day;
}

二、js获取当前月份的最后一天

function getCurrentMonthLast(){
        var date=new Date();
        var currentMonth=date.getMonth();
        var nextMonth=++currentMonth;
        var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
        var oneDay=1000*60*60*24;
        var lastTime = new Date(nextMonthFirstDay-oneDay);
        var month = parseInt(lastTime.getMonth()+1);
        var day = lastTime.getDate();
        if (month < 10) {
            month = '0' + month
        }
        if (day < 10) {
            day = '0' + day
        }
        return date.getFullYear() + '-' + month + '-' + day;
    }

三、获取本周第一天

showWeekFirstDay() {
  var Nowdate = new Date();
  var WeekFirstDay = new Date(Nowdate - (Nowdate.getDay() - 1) * 86400000);
  var M = Number(WeekFirstDay.getMonth()) + 1;
  var D = WeekFirstDay.getDate();
  M = M < 10 ? "0" + M : M;
  D = D < 10 ? "0" + D : D;
  return WeekFirstDay.getFullYear() + "-" + M + "-" + D;
}

四、获取本周最后一天

 showWeekLastDay() {
      var Nowdate = new Date();
      var WeekFirstDay = new Date(Nowdate - (Nowdate.getDay() - 1) * 86400000);
      var WeekLastDay = new Date((WeekFirstDay / 1000 + 6 * 86400) * 1000);
      var M = Number(WeekLastDay.getMonth()) + 1;
      var D = WeekLastDay.getDate();
      M = M < 10 ? "0" + M : M;
      D = D < 10 ? "0" + D : D;
      return WeekLastDay.getFullYear() + "-" + M + "-" + D;
    },

五、JS 获取当前年份,月份

function doHandleDate() {
var myDate = new Date();
var tYear = myDate.getFullYear();
var tMonth = myDate.getMonth();

var m = tMonth + 1;
if (m.toString().length == 1) {
m = "0" + m;
}
return tYear +'-'+ m;
}
function doHandleYear(tYear) {
var myDate = new Date();
var tYear = myDate.getFullYear();

return tYear;
}
function doHandleMonth() {
var myDate = new Date();
var tMonth = myDate.getMonth();

var m = tMonth + 1;
if (m.toString().length == 1) {
m = "0" + m;
}
return m;
}

六、获取完整的日期

 var date=new Date;
 var year=date.getFullYear(); 
 var month=date.getMonth()+1;
 month =(month<10 ? "0"+month:month); 
 var mydate = (year.toString()+month.toString());

注意,year.toString()+month.toString()不能写成year+month。不然如果月份大于等于10,则月份为数字,会和年份相加,如201210,则会变为2022,需要加.toString()

以下是搜到的有用内容:

var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间

<SCRIPT LANGUAGE="JavaScript">
function monthnow(){
 var now   = new Date();
 var monthn = now.getMonth();
 var yearn  = now.getYear();
 window.location.href="winnNamelist.jsp?getMonth="+monthn+"&getYear="+yearn;
}

  // 时间处理 加一天
    addDate(date, days) {
      var d = new Date(date);
      d.setDate(d.getDate() + days);
      var month = d.getMonth() + 1;
      var day = d.getDate();
      if (month < 10) {
        month = "0" + month;
      }
      if (day < 10) {
        day = "0" + day;
      }
      var val = d.getFullYear() + "" + month + "" + day;
      return val;
    },

标签:date,Date,month,获取,时间,var,new,JS,day
来源: https://www.cnblogs.com/wml-it/p/16573629.html