获取今天,昨天,本周,上周,本月,上月时间(实例分享)


Posted in Javascript onJanuary 04, 2017

话不多说,请看代码:

//获取今天
var nowDate= new Date(); //当天日期
console.log(nowDate);
//今天是本周的第几天
var nowDayOfWeek= nowDate.getDay();
console.log(nowDayOfWeek);
//当前日
var nowDay = nowDate.getDate();
console.log(nowDay);
//当前月
var nowMonth = nowDate.getMonth();
console.log(nowMonth);
//当前年
var nowYear = nowDate.getFullYear();
console.log(nowYear);
//var nowHours = nowDate.getHours();
//var nowMinutes = nowDate.getMinutes();
//var nowSeconds = nowDate.getSeconds();
nowYear += (nowYear < 2000) ? 1900 : 0; //
console.log(nowYear);
var lastMonthDate = new Date(); //上月日期
console.log(lastMonthDate);
lastMonthDate.setDate(1);
console.log(lastMonthDate.setDate(1));
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
console.log(lastMonthDate.setMonth(lastMonthDate.getMonth()-1));
var lastYear = lastMonthDate.getYear();
console.log(lastYear);
var lastMonth = lastMonthDate.getMonth();
console.log(lastMonth);
//格式化日期:yyyy-MM-dd
function formatDate(date) {
 var myyear = date.getFullYear();
 var mymonth = date.getMonth()+1;
 var myweekday = date.getDate();
 //var myHours = date.getHours();
 //var myMinutes = date.getMinutes();
 //var mySeconds = date.getSeconds();
 if(mymonth < 10){
 mymonth = "0" + mymonth;
 }
 if(myweekday < 10){
 myweekday = "0" + myweekday;
 }
 //if(myHours < 10){
 // myHours = "0" + myHours;
 //}
 //if(myMinutes < 10){
 // myMinutes = "0" + myMinutes;
 //}
 return (myyear+"/"+mymonth + "/" + myweekday);
 //return (myyear+"/"+mymonth + "/" + myweekday + " " + myHours+ ":" + myMinutes);
}
//获得某月的天数
function getMonthDays(myMonth){
 var monthStartDate = new Date(nowYear, myMonth, 1);
 var monthEndDate = new Date(nowYear, myMonth + 1, 1);
 var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
 return days;
}
////获得本季度的开始月份
//function getQuarterStartMonth(){
// var quarterStartMonth = 0;
// if(nowMonth<3){
// quarterStartMonth = 0;
// }
// if(2<6){
// quarterStartMonth = 3;
// }
// if(5<9){
// quarterStartMonth = 6;
// }
// if(nowMonth>8){
// quarterStartMonth = 9;
// }
// return quarterStartMonth;
//}
//今天
$scope.toDay = function(){
 var getCurrentDate = new Date();
 var getCurrentDate = formatDate(getCurrentDate);
 $scope.today = getCurrentDate;
 console.log($scope.today);
 $("#jqueryPickerTime3").val($scope.today);
 $("#jqueryPickerTime4").val($scope.today);
};
//昨天
$scope.yesTerDay = function(){
 var getYesterdayDate = new Date(nowYear, nowMonth, nowDay - 1);
 var getYesterdayDate = formatDate(getYesterdayDate);
 $scope.yesTday = getYesterdayDate;
 console.log(getYesterdayDate);
 $("#jqueryPickerTime3").val($scope.yesTday);
 $("#jqueryPickerTime4").val($scope.yesTday);
};
//获得本周的开始日期
$scope.thisWeek = function(){
 var getWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
 var getWeekStartDate = formatDate(getWeekStartDate);
 $scope.tswkStart = getWeekStartDate;
 console.log($scope.tswkStart);
 $("#jqueryPickerTime3").val($scope.tswkStart);
 //获得本周的结束日期
 var getWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
 var getWeekEndDate = formatDate(getWeekEndDate);
 $scope.tswkEnd = getWeekEndDate;
 console.log($scope.tswkEnd);
 $("#jqueryPickerTime4").val($scope.tswkEnd);
};
$scope.lastWeek = function(){
 //获得上周的开始日期
 var getUpWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek -7);
 var getUpWeekStartDate = formatDate(getUpWeekStartDate);
 $scope.startLastWeek = getUpWeekStartDate;
 console.log($scope.startLastWeek);
 $("#jqueryPickerTime3").val($scope.startLastWeek);
 //获得上周的结束日期
 var getUpWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek - 7));
 var getUpWeekEndDate = formatDate(getUpWeekEndDate);
 $scope.endLastWeek = getUpWeekEndDate;
 console.log($scope.endLastWeek);
 $("#jqueryPickerTime4").val($scope.endLastWeek);
};
//本月
$scope.thisMonth = function(){
 //获得本月的开始日期
 var getMonthStartDate = new Date(nowYear, nowMonth, 1);
 var getMonthStartDate = formatDate(getMonthStartDate);
 $scope.startThisMonth = getMonthStartDate;
 console.log($scope.startThisMonth);
 $("#jqueryPickerTime3").val($scope.startThisMonth);
 //获得本月的结束日期
 var getMonthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
 var getMonthEndDate = formatDate(getMonthEndDate);
 $scope.endThisMonth = getMonthEndDate;
 console.log($scope.endThisMonth);
 $("#jqueryPickerTime4").val($scope.endThisMonth);
};
//上月
$scope.lastMonth = function(){
 //获得上月开始时间
 var getLastMonthStartDate = new Date(nowYear, lastMonth+1, 1);
 var getLastMonthStartDate = formatDate(getLastMonthStartDate);
 $scope.startLastMonth = getLastMonthStartDate;
 console.log($scope.startLastMonth);
 $("#jqueryPickerTime3").val($scope.startLastMonth);
 //获得上月结束时间
 var getLastMonthEndDate = new Date(nowYear, lastMonth+1, getMonthDays(lastMonth+1));
 var getLastMonthEndDate = formatDate(getLastMonthEndDate);
 $scope.endLastMonth = getLastMonthEndDate;
 console.log($scope.endLastMonth);
 $("#jqueryPickerTime4").val($scope.endThisMonth);
};

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

Javascript 相关文章推荐
原创javascript小游戏实现代码
Aug 19 Javascript
使用jquery 简单实现下拉菜单
Jan 14 Javascript
正则表达式优化JSON字符串的技巧
Dec 24 Javascript
在 Angular 中使用Chart.js 和 ng2-charts的示例代码
Aug 17 Javascript
详解如何实现一个简单的 vuex
Feb 10 Javascript
Angularjs Promise实例详解
Mar 15 Javascript
在vue 中使用 less的教程详解
Sep 26 Javascript
JavaScript使用小插件实现倒计时的方法讲解
Mar 11 Javascript
基于Vue2-Calendar改进的日历组件(含中文使用说明)
Apr 14 Javascript
记录一次开发微信网页分享的步骤
May 07 Javascript
Vue 权限控制的两种方法(路由验证)
Aug 16 Javascript
微信小程序间使用navigator跳转传值问题实例分析
Mar 27 Javascript
手机端js和html5刮刮卡效果
Sep 29 #Javascript
JQuery和HTML5 Canvas实现弹幕效果
Jan 04 #Javascript
laydate.js日期时间选择插件
Jan 04 #Javascript
AngularJS使用ng-app自动加载bootstrap框架问题分析
Jan 04 #Javascript
微信小程序 解决swiper不显示图片的方法
Jan 04 #Javascript
bootstrap laydate日期组件使用详解
Jan 04 #Javascript
BootStrap 模态框实现刷新网页并关闭功能
Jan 04 #Javascript
You might like
php中使用ExcelFileParser处理excel获得数据(可作批量导入到数据库使用)
2010/08/21 PHP
php 数组的一个悲剧?
2011/05/11 PHP
PHP实现支持GET,POST,Multipart/form-data的HTTP请求类
2014/09/24 PHP
php天翼开放平台短信发送接口实现方法
2014/12/22 PHP
PHP框架性能测试报告
2016/05/08 PHP
详解CSS样式中的 !important * _ 符号
2021/03/09 HTML / CSS
在IE模态窗口中自由查看HTML源码的方法
2007/03/08 Javascript
动态创建的表格单元格中的事件实现代码
2008/12/30 Javascript
event.srcElement 用法笔记e.target
2009/12/18 Javascript
JS在TextArea光标位置插入文字并实现移动光标到文字末尾
2013/06/21 Javascript
JS实现图片横向滚动效果示例代码
2013/09/04 Javascript
原生Javascript封装的一个AJAX函数分享
2014/10/11 Javascript
JavaScript中的比较操作符&gt;、=、
2014/12/31 Javascript
浅谈jQuery构造函数分析
2015/05/11 Javascript
Struts2+jquery.form.js实现图片与文件上传的方法
2016/05/05 Javascript
详解node.js平台下Express的session与cookie模块包的配置
2017/04/26 Javascript
微信小程序开发之实现自定义Toast弹框
2017/06/08 Javascript
JavaScript 中Date对象的格式化代码方法汇总
2017/09/06 Javascript
在vue里面设置全局变量或数据的方法
2018/03/09 Javascript
jQuery基于闭包实现的显示与隐藏div功能示例
2018/06/09 jQuery
Vue toFixed保留两位小数的3种方式
2020/10/23 Javascript
[03:56]DOTA2完美大师赛趣味视频之小鸽子和Mineski打台球
2017/11/24 DOTA
[57:29]Alliance vs KG 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/17 DOTA
Python中动态检测编码chardet的使用教程
2017/07/06 Python
使用pickle存储数据dump 和 load实例讲解
2019/12/30 Python
win10安装tesserocr配置 Python使用tesserocr识别字母数字验证码
2020/01/16 Python
Python基于requests库爬取网站信息
2020/03/02 Python
Python如何实现后端自定义认证并实现多条件登陆
2020/06/22 Python
利用Python实现最小二乘法与梯度下降算法
2021/02/21 Python
python 实现网易邮箱邮件阅读和删除的辅助小脚本
2021/03/01 Python
企业安全生产承诺书
2014/05/22 职场文书
2014离婚协议书范文
2014/09/10 职场文书
社区党员群众路线教育实践活动心得体会
2014/11/03 职场文书
成事在人观后感
2015/06/16 职场文书
MongoDB连接数据库并创建数据等使用方法
2021/11/27 MongoDB
Android实现获取短信验证码并自动填充
2023/05/21 Java/Android