zoukankan      html  css  js  c++  java
  • 常用的时间函数整理

      这次的项目中,有很多用到了时间函数,报错直接获取本月的开始日期,结束如期。本周的开始时间,结束时间等。这里简单的记录一下,方便下次引用。时间格式大家可以自行修改,例子中都是格式化成为了2019-07-01 15:55:00这样的格式。

    1. 最常用的一个,就是对JS原生new Date()的扩展,可以格式成为自己想要的格式。(以下的函数都是需要调用这个的,所以必须要引入这个)
     1 /**
     2 * 对Date的扩展,将 Date 转化为指定格式的String
     3 * 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
     4 * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
     5 * 例子: 
     6 * (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
     7 * (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
     8 */
     9 Date.prototype.Format = function (fmt) { //author: meizz 
    10     var o = {
    11         "M+": this.getMonth() + 1, //月份 
    12         "d+": this.getDate(), //
    13         "h+": this.getHours(), //小时 
    14         "m+": this.getMinutes(), //
    15         "s+": this.getSeconds(), //
    16         "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
    17         "S": this.getMilliseconds() //毫秒 
    18     };
    19     if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    20     for (var k in o)
    21     if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    22     return fmt;
    23 }

      2.获取这个月,或者上个月,或者下个月的开始日期和结束日期,大家简单的修改,就能获取到本月的开始时间和结束时间

     1 //获取当月或者上个月数据的开始日期和结束日期 get_date(0):当月的开始和结束 get_date(-1):上个月的开始和结束日期
     2         function get_date(addMonth){ 
     3             var now_date = new Date();
     4             var month = now_date.getMonth();
     5             var year = now_date.getFullYear();
     6             var this_yue_begin = new Date(new Date(year,month,1)).Format("yyyy-MM-dd");
     7             var this_yue_end = "";
     8             var set_yue_begin = "";
     9             var set_yue_end = "";
    10             if(addMonth){
    11                 if((month+addMonth)>=12){
    12                     set_yue_begin = new Date(new Date(year+1,(month+addMonth-12),1)).Format("yyyy-MM-dd");
    13                 }else if((month+addMonth)<=0){
    14                     set_yue_begin = new Date(new Date(year-1,11,1)).Format("yyyy-MM-dd");
    15                 }else{
    16                     set_yue_begin = new Date(new Date(year,month+addMonth,1)).Format("yyyy-MM-dd");
    17                 }
    18                 
    19                 if(month+addMonth==11){
    20                     set_yue_end = new Date(new Date(year+1,0,1).getTime()-1000).Format("yyyy-MM-dd");
    21                 }else{
    22                     set_yue_end = new Date(new Date(year,month+addMonth+1,1).getTime()-1000).Format("yyyy-MM-dd");
    23                 }
    24                 return set_yue_begin+" 00:00:00 - "+set_yue_end+" 23:59:59";
    25             }else{
    26                 if(month==11){
    27                     this_yue_end = new Date(new Date(year+1,0,1).getTime()-1000).Format("yyyy-MM-dd");
    28                 }else{
    29                     this_yue_end = new Date(new Date(year,month+1,1).getTime()-1000).Format("yyyy-MM-dd");
    30                 }
    31                 return this_yue_begin +" 00:00:00 - "+this_yue_end+" 23:59:59";
    32             }
    33         }

      3.获取本周的开始时间和结束时间

     1 //获取本周的开始和结束时间,里面也是传0是获取本周的时间,传-1是获取上一周的
     2         function getWeekStartAndEnd(AddWeekCount) { 
     3             //起止日期数组   
     4             var startStop = new Array(); 
     5             //一天的毫秒数   
     6             var millisecond = 1000 * 60 * 60 * 24; 
     7             //获取当前时间   
     8             var currentDate = new Date();
     9             //相对于当前日期AddWeekCount个周的日期
    10             currentDate = new Date(currentDate.getTime() + (millisecond * 7*AddWeekCount));
    11             //返回date是一周中的某一天
    12             var week = currentDate.getDay(); 
    13             //返回date是一个月中的某一天   
    14             var month = currentDate.getDate();
    15             //减去的天数   
    16             var minusDay = week != 0 ? week - 1 : 6; 
    17             //获得当前周的第一天   
    18             var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay)); 
    19             //获得当前周的最后一天
    20             var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6));
    21             //添加至数组   
    22             startStop.push(getDateStr3(currentWeekFirstDay)); 
    23             startStop.push(getDateStr3(currentWeekLastDay)); 
    24         
    25             return getDateStr3(currentWeekFirstDay)+" 00:00:00 - "+getDateStr3(currentWeekLastDay)+" 23:59:59"; 
    26         }
    27         function getDateStr3(date) {
    28             var year = "";
    29             var month = "";
    30             var day = "";
    31             var now = date;
    32             year = ""+now.getFullYear();
    33             if((now.getMonth()+1)<10){
    34                 month = "0"+(now.getMonth()+1);
    35             }else{
    36                 month = ""+(now.getMonth()+1);
    37             }
    38             if((now.getDate())<10){
    39                 day = "0"+(now.getDate());
    40             }else{
    41                 day = ""+(now.getDate());
    42             }
    43             return year+"-"+month+"-"+day;
    44         }

      4.获取最近三十天的时间

    1 function get_30_date(){
    2             var now_date = new Date().Format("yyyy-MM-dd hh:mm:ss");
    3             var ago_30 = new Date(new Date().getTime()-30*24*60*60*1000).Format("yyyy-MM-dd hh:mm:ss");
    4             return ago_30+" - "+now_date;
    5         }
  • 相关阅读:
    gridFS-Nginx的安装与使用
    centos下利用phantomjs来完成网站页面快照截图
    linux下安装php的svn模块
    在Thinkphp3.1中使用Mongo的具体操作
    CentOS 6.4安装mongo的php扩展包
    在centos6.3下安装php的Xdebug
    在yum安装lamp的环境下安装coreseek以及php的sphinx扩展
    CentOS 6.4下通过YUM快速安装配置LAMP服务器(Apache+PHP5+MySQL)
    微信中web页面实现和公众号中查看图片一样的效果
    ionic学习教程地址梳理
  • 原文地址:https://www.cnblogs.com/daniao11417/p/11114586.html
Copyright © 2011-2022 走看看