1 //获取今天
  2 var nowDate= new Date(); //当天日期
  3 console.log(nowDate);
  4 //今天是本周的第几天
  5 var nowDayOfWeek= nowDate.getDay();
  6 console.log(nowDayOfWeek);
  7 //当前日
  8 var nowDay = nowDate.getDate();
  9 console.log(nowDay);
 10 //当前月
 11 var nowMonth = nowDate.getMonth();
 12 console.log(nowMonth);
 13 //当前年
 14 var nowYear = nowDate.getFullYear();
 15 console.log(nowYear);
 16 //var nowHours = nowDate.getHours();
 17 //var nowMinutes = nowDate.getMinutes();
 18 //var nowSeconds = nowDate.getSeconds();
 19
 20 nowYear += (nowYear < 2000) ? 1900 : 0;  //
 21 console.log(nowYear);
 22
 23
 24 var lastMonthDate = new Date();  //上月日期
 25 console.log(lastMonthDate);
 26
 27 lastMonthDate.setDate(1);
 28 console.log(lastMonthDate.setDate(1));
 29
 30 lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
 31 console.log(lastMonthDate.setMonth(lastMonthDate.getMonth()-1));
 32
 33 var lastYear = lastMonthDate.getYear();
 34 console.log(lastYear);
 35
 36 var lastMonth = lastMonthDate.getMonth();
 37 console.log(lastMonth);
 38
 39
 40 //格式化日期:yyyy-MM-dd
 41 function formatDate(date) {
 42   var myyear = date.getFullYear();
 43   var mymonth = date.getMonth()+1;
 44   var myweekday = date.getDate();
 45   //var myHours = date.getHours();
 46   //var myMinutes = date.getMinutes();
 47   //var mySeconds = date.getSeconds();
 48
 49   if(mymonth < 10){
 50     mymonth = "0" + mymonth;
 51   }
 52   if(myweekday < 10){
 53     myweekday = "0" + myweekday;
 54   }
 55   //if(myHours < 10){ 56   //  myHours = "0" + myHours;
 57   //}
 58   //if(myMinutes < 10){ 59   //  myMinutes = "0" + myMinutes;
 60   //}
 61   return (myyear+"/"+mymonth + "/" + myweekday);
 62   //return (myyear+"/"+mymonth + "/" + myweekday + " " + myHours+ ":" + myMinutes);
 63 }
 64
 65 //获得某月的天数
 66 function getMonthDays(myMonth){
 67   var monthStartDate = new Date(nowYear, myMonth, 1);
 68   var monthEndDate = new Date(nowYear, myMonth + 1, 1);
 69   var   days   =   (monthEndDate   -   monthStartDate)/(1000   *   60   *   60   *   24);
 70   return   days;
 71 }
 72
 73 ////获得本季度的开始月份
 74 //function getQuarterStartMonth(){ 75 //  var quarterStartMonth = 0;
 76 //  if(nowMonth<3){ 77 //    quarterStartMonth = 0;
 78 //  }
 79 //  if(2<6){ 80 //    quarterStartMonth = 3;
 81 //  }
 82 //  if(5<9){ 83 //    quarterStartMonth = 6;
 84 //  }
 85 //  if(nowMonth>8){ 86 //    quarterStartMonth = 9;
 87 //  }
 88 //  return quarterStartMonth;
 89 //}
 90
 91
 92 //今天
 93 $scope.toDay = function(){
 94   var getCurrentDate = new Date();
 95   var getCurrentDate  = formatDate(getCurrentDate);
 96   $scope.today = getCurrentDate;
 97   console.log($scope.today);
 98   $("#jqueryPickerTime3").val($scope.today);
 99   $("#jqueryPickerTime4").val($scope.today);
100 };
101
102 //昨天
103 $scope.yesTerDay = function(){
104   var getYesterdayDate = new Date(nowYear, nowMonth, nowDay - 1);
105   var getYesterdayDate =  formatDate(getYesterdayDate);
106
107   $scope.yesTday = getYesterdayDate;
108   console.log(getYesterdayDate);
109   $("#jqueryPickerTime3").val($scope.yesTday);
110   $("#jqueryPickerTime4").val($scope.yesTday);
111 };
112
113
114 //获得本周的开始日期
115 $scope.thisWeek = function(){
116   var getWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
117   var getWeekStartDate =  formatDate(getWeekStartDate);
118   $scope.tswkStart = getWeekStartDate;
119   console.log($scope.tswkStart);
120   $("#jqueryPickerTime3").val($scope.tswkStart);
121   //获得本周的结束日期
122   var getWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
123   var getWeekEndDate =  formatDate(getWeekEndDate);
124   $scope.tswkEnd = getWeekEndDate;
125   console.log($scope.tswkEnd);
126   $("#jqueryPickerTime4").val($scope.tswkEnd);
127 };
128
129 $scope.lastWeek = function(){
130   //获得上周的开始日期
131   var getUpWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek -7);
132   var getUpWeekStartDate =  formatDate(getUpWeekStartDate);
133   $scope.startLastWeek = getUpWeekStartDate;
134   console.log($scope.startLastWeek);
135   $("#jqueryPickerTime3").val($scope.startLastWeek);
136   //获得上周的结束日期
137   var getUpWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek - 7));
138   var getUpWeekEndDate =  formatDate(getUpWeekEndDate);
139   $scope.endLastWeek = getUpWeekEndDate;
140   console.log($scope.endLastWeek);
141   $("#jqueryPickerTime4").val($scope.endLastWeek);
142
143
144 };
145 //本月
146 $scope.thisMonth  = function(){
147   //获得本月的开始日期
148   var getMonthStartDate = new Date(nowYear, nowMonth, 1);
149   var getMonthStartDate =  formatDate(getMonthStartDate);
150   $scope.startThisMonth = getMonthStartDate;
151   console.log($scope.startThisMonth);
152   $("#jqueryPickerTime3").val($scope.startThisMonth);
153
154   //获得本月的结束日期
155   var getMonthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
156   var getMonthEndDate =  formatDate(getMonthEndDate);
157   $scope.endThisMonth = getMonthEndDate;
158   console.log($scope.endThisMonth);
159   $("#jqueryPickerTime4").val($scope.endThisMonth);
160 };
161 //上月
162 $scope.lastMonth = function(){
163   //获得上月开始时间
164   var getLastMonthStartDate = new Date(nowYear, lastMonth+1, 1);
165   var getLastMonthStartDate = formatDate(getLastMonthStartDate);
166
167   $scope.startLastMonth = getLastMonthStartDate;
168   console.log($scope.startLastMonth);
169
170   $("#jqueryPickerTime3").val($scope.startLastMonth);
171   //获得上月结束时间
172   var getLastMonthEndDate = new Date(nowYear, lastMonth+1, getMonthDays(lastMonth+1));
173   var getLastMonthEndDate = formatDate(getLastMonthEndDate);
174
175   $scope.endLastMonth = getLastMonthEndDate;
176   console.log($scope.endLastMonth);
177   $("#jqueryPickerTime4").val($scope.endThisMonth);
178 };

转载于:https://www.cnblogs.com/yaomin/p/6253146.html

获取今天,昨天,本周,上周,本月,上月时间相关推荐

  1. 【SQL】获取今天昨天本周上周本月上月本年去年的起止日期

    文章目录 SQL获取日期 SQL获取今天日期 SQL获取昨天日期 SQL获取上周日期,上周一,上周末 SQL获取本周日期,本周一,本周末 SQL获取上月日期,上月初,上月末 SQL获取本月日期,本月初 ...

  2. php 获取时间段 今天昨天本周上周本月上月本季度本年去年

    /**获取时间段* @param $name* @param $param*/ function gettimestamp($name = 'today',$param = ''){switch ($ ...

  3. 用php获取本周,上周,本月,上月,本季度日期的代码

    1. 前言 ADODB 是 Active Data Objects Data Base 的简称,它是一种 PHP 存取数据库的函式组件.现在 SFS3 系统 (校园自由软件交流网学务系统) 计划的主持 ...

  4. python获取当日、本周、本月、昨日、上周、上月时间

    获取当日.本周.本月.昨日.上周.上月时间 关注公众号"轻松学编程"了解更多. import datetimedef get_current_day():'''当日:return: ...

  5. js实现获取今日、本周、本月、本学期的开始结束日期

    一.js实现获取今日.本周.本月.本学期的开始结束日期 getDateFn.js 今日.本周.本月,实现是参考他人资源! <template><div> </div> ...

  6. php 本季度时间戳,PHP获取今日、本周、本月、本季度的起止时间戳

    有的时候需要查询数据库特定时间段的记录,比如,获取今日.本周.本月.本季度的数据,这就需要用到php的date()函数和 maketime()函数了,下面先简单的介绍下这2个函数的用法,然后直接上代码 ...

  7. mysql获取当天,昨天,本周,本月,上周,上月的起始时间

    -- 今天 SELECT DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00') AS '今天开始'; SELECT DATE_FORMAT(NOW(),'%Y-%m-%d 23 ...

  8. 获取今日、本周、本月、上月、今年的日期

    1.首先格式化时间 // 格式化时间 function formatDate(date) { var myyear = date.getFullYear(); var mymonth = date.g ...

  9. java 本季度_Java获取当天、本周、本月、本季度、本年等 开始及结束时间

    package com.zhaochao.utils; import java.text.SimpleDateFormat; import java.util.Calendar; import jav ...

  10. java 获取季度第一天_Java获取当天、本周、本月、本季度、本年等 开始及结束时间...

    package com.zhaochao.utils; import java.text.SimpleDateFormat; import java.util.Calendar; import jav ...

最新文章

  1. 2021年大数据Spark(一):框架概述
  2. mysql全量和增量备份脚本
  3. android 网络时区 错误,React native 安卓机器上调试代码报错:网络请求出错TypeError: Network request failed...
  4. 计算机科学与编程基础,国外经典教材·计算机科学与技术:Oracle 10g编程基础
  5. 数字图像处理吴娱课后答案_数字图像处理课后题答案
  6. Ajax联手SOA打造企业级应用
  7. 鸿蒙电脑系统什么时候普及,鸿蒙操作系统开源是什么意思?华为鸿蒙系统什么时候能用...
  8. fc安卓模拟器_MAME街机模拟器0.224经典游戏全收藏
  9. 怎样把英文pdf翻译成中文?
  10. 企业信用等级重不重要?看看各地招标文件就知道了。
  11. IBDP1 经济好学吗?学习建议分享
  12. 奶瓶(beini) 又一蹭网神器 1.2.2增强版带600万密码字典
  13. velodyne VLP-16线三维雷达在ros使用
  14. 解决fatal error C1060: 编译器的堆空间不足(详解)
  15. c语言中出现void错误,关于C语言问题
  16. Linux 命令别名 alias(含“永久生效”方法)
  17. ubuntu20.04安装cuda库
  18. 怎么防止解决百度转码问题
  19. 视频系统部署 kvs
  20. 数据越多,AI决策模型越脆弱

热门文章

  1. 关于FragmentPager实现Fragment的滑动切换
  2. 计算机课搞事情检讨,上微机课玩游戏检讨书
  3. c语言程序设计实验报告2,C语言程序设计实验报告2.docx
  4. 计算机行业常见英语,计算机行业常用英语词汇
  5. nginx 根据IP 进行灰度发布
  6. 修改input标签输入样式
  7. redis 安装-配置
  8. Python入门学习笔记11(静态方法、类方法与属性方法)
  9. 在腾讯云上安装mysql遇到的问题
  10. 查找字符串中首个非重复字符