网络编程 
首页 > 网络编程 > 浏览文章

js获取本日、本周、本月的时间代码

(编辑:jimmy 日期: 2024/11/26 浏览:3 次 )

本日时间

function showToDay()
{   
  var Nowdate=new Date();   
  M=Number(Nowdate.getMonth())+1   
  alert(Nowdate.getMonth()+"月,"+Nowdate.getDate()+"号,星期"+Nowdate.getDay());   
  return Nowdate.getYear()+"-"+M+"-"+Nowdate.getDate();   
}

昨天

function showTomorrow()
{   
  var tom=new Date();   
  tom.setDate(tom.getDate()+1);   
  M=Number(tom.getMonth())+1   
  return tom.getYear()+"-"+M+"-"+tom.getDate();   
}

本周第一天

function showWeekFirstDay()   
{   
  var Nowdate=new Date();   
  var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);   
  M=Number(WeekFirstDay.getMonth())+1   
  return WeekFirstDay.getYear()+"-"+M+"-"+WeekFirstDay.getDate();   
}

本周最后天

function showWeekLastDay()   
{   
  var Nowdate=new Date();   
  var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);   
  var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);   
  M=Number(WeekLastDay.getMonth())+1   
  return WeekLastDay.getYear()+"-"+M+"-"+WeekLastDay.getDate();   
}

本月第一天

function showMonthFirstDay()   
{   
  var Nowdate=new Date();   
  var MonthFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth(),1);   
  M=Number(MonthFirstDay.getMonth())+1   
  return MonthFirstDay.getYear()+"-"+M+"-"+MonthFirstDay.getDate();   
}

本月最后一天

function showMonthLastDay()   
{   
  var Nowdate=new Date();   
  var MonthNextFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth()+1,1);   
  var MonthLastDay=new Date(MonthNextFirstDay-86400000);   
  M=Number(MonthLastDay.getMonth())+1   
  return MonthLastDay.getYear()+"-"+M+"-"+MonthLastDay.getDate();   
}

function getTime(s)
{   
  var time1=document.getElementById("num1");   
  var time2=document.getElementById("num2");   
  if(s==1){   
    //本日时间   
    time1.value=showToDay();   
    //alert("今日:"+time1.value);   
    time2.value=showTomorrow();   
    //alert("今日:"+time2.value);   
  }else if(s==2){   
    //本周时间   
    time1.value=showWeekFirstDay();   
    //alert("本月第一天:"+time1.value);   
    time2.value=showWeekLastDay();   
    //alert("本月最后一天:"+time2.value);   
  }else{   
    //本月时间   
    time1.value=showMonthFirstDay();   
    //alert("本月第一天"+time1.value);   
    time2.value=showMonthLastDay();   
    //alert("本月最后一天"+time2.value);     
 }
}

其他网友补充的完整测试代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
<input type="text" id="dateFrom" value="">
<input type="text" id="dateTo" value="">
<script>
window.onload = function() {
  // getDate("thisDay");
  // getDate("thisWeek");
  // getDate("thisMonth");
  getDate("thisYear");
}  
  //获取推送时间段 
  function getDate(type){ 
    var kssj,jssj; 
    var today = new Date();     
    var DAY = 1000*60*60*24;  
    if(type =="thisDay") { //当天
      kssj = new Date();  
      jssj = new Date();  
    }  
    if(type == "thisWeek") { //本周周
      kssj = getThisWeekFirstDay();//本周第一天 
      jssj = new Date(); 
    } 
    if(type == "thisMonth") { //本月 
      kssj = getThisMonthFirstDay();//本月第一天 
      jssj = new Date(); 
    } 
    if(type == "thisYear") {
      kssj = getThisYearFirstDay();//本年第一天
      jssj = new Date();
    }
    var dateFrom = kssj.format('yyyy-MM-dd');
    var dateTo = jssj.format('yyyy-MM-dd');
    document.getElementById("dateFrom").value=dateFrom;
    document.getElementById("dateTo").value=dateTo;
  } 
  function getThisWeekFirstDay() {
    var now = new Date();
    var day = now.getDay();
    var week = "1234567";
    var first = 0 - week.indexOf(day);
    var thisWeekfirstDay = new Date;
    thisWeekfirstDay.setDate (thisWeekfirstDay.getDate () + first);
    return thisWeekfirstDay;
  } 
  function getThisMonthFirstDay(){ 
    var date=new Date();
    date.setDate(1);
    return date; 
  } 
  function getThisYearFirstDay() { 
    var currentDate=new Date(); 
    var currentYear=currentDate.getFullYear(); 
    var currentYearFirstDate=new Date(currentYear,0,1);
    return currentYearFirstDate;
  }
  //data类型转string 
  Date.prototype.format = function(format) 
  { 
    var o = { 
      "M+" : this.getMonth()+1, //month 
      "d+" : this.getDate(),  //day 
      "h+" : this.getHours(),  //hour 
      "m+" : this.getMinutes(), //minute 
      "s+" : this.getSeconds(), //second 
      "q+" : Math.floor((this.getMonth()+3)/3), //quarter 
      "S" : this.getMilliseconds() //millisecond 
    } 
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1, 
    (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
    for(var k in o)if(new RegExp("("+ k +")").test(format)) 
      format = format.replace(RegExp.$1, 
        RegExp.$1.length==1 "00"+ o[k]).substr((""+ o[k]).length)); 
    return format; 
  } 
</script>
</body>
</html>

这样基本上就解决问题了

上一篇:js 计算月/周的第一天和最后一天代码
下一篇:js 获取本周、上周、本月、上月、本季度、上季度的开始结束日期
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap