JS时间操作大全

1、获取每个月的开始和结束。

2、获取每个季度的开始和结束。

3、获取当前季度。

4、把日期转换为字符串(支持各种格式)

...

5、未完待续,不断添加
 String.prototype.padingStar=function(totalLength,padingStr="") {
            if (totalLength <= this.length) {
                return this;
            }
            var padLength = totalLength - this.length;
            if (padLength <= padingStr.length) {
                return padingStr.substring(0, padLength) + this;
            } else {
                var len = padLength / padingStr.length, n = 1,str='';
                while (n<len) {
                    str += padingStr;
                    n++;
                }
                return str + padingStr.substring(0, padLength - (n-1) * padingStr.length) +this;
            }
        }

        String.prototype.padingEnd = function (totalLength, padingStr="") {
            //在开始的补全后面
            if (totalLength <= this.length) {
                return this;
            }
            var padLength = totalLength - this.length;
            if (padLength <= padingStr.length) {
                return padingStr.substring(0, padLength) + this;
            } else {
                var len = padLength / padingStr.length, n = 0,str='';
                while (n<len) {
                    str += padingStr;
                    n++;
                }
                return this + padingStr.substring(0, padLength - (n - 1) * padingStr.length)+str;
            }
        }

        //获取当前月的开始
        Date.prototype.starOfMonth=function() {
            return new Date(this.getFullYear(), this.getMonth(), 1, 00, 00, 00);
        }

        //获取当前月的结束
        Date.prototype.endOfMonth=function() {
            return new Date(this.getFullYear(), this.getMonth() + 1, 0, 23, 59, 59);
        }

        //获取当前季度的开始时间
        Date.prototype.starofQuarter=function() {
            return new Date(this.getFullYear(), (this.getQuarter() - 1) * 3, 01, 00, 00, 00);
        }

        //获取当前季度的结束时间
        Date.prototype.endofQuarter=function() {
            return new Date(this.getFullYear(), this.getQuarter() * 3-1 , 31, 23, 59, 59);
        }

        //获取当前时间对应的季度
        Date.prototype.getQuarter=function() {
            return Math.ceil((this.getMonth() + 1) / 3);
        }

        //获取当前时间对应的年的开始
        Date.prototype.starOfYear=function() {
            return new Date(this.getFullYear(), 01, 01, 00, 00, 00);
        } 

        //获取当前时间对应年的结束
        Date.prototype.endOfYear=function() {
            return new Date(this.getFullYear(), 12, 31, 23, 59, 59);
        }

        //把时间格式化为字符串
        Date.prototype.toDateString = function(format) {
            if (typeof (format) == "undefined") {
                return this.toString();
            }
            //可能我的第一个想法,就是
            if (/y{4}/.test(format)) {
                format = format.replace(/yyyy/g, this.getFullYear());
            }
            if (/y{2}/.test(format)) {
                format = format.replace(/y{2}/g,this.getFullYear().toString().substr(2));
            }
            if (/M{2}/.test(format)) {
                format = format.replace(/MM/,this.getMonth().toString().padingStar(2,0));
            }
            if (/dd/.test(format)) {
                format = format.replace(/dd/,this.getDate().toString().padingStar(2,'0'));
            }
            if (/HH/.test(format)) {
                format = format.replace(/HH/g, this.getHours().toString().padingStar(2, '0'));
            }
            if (/hh/.test(format)) {
                format = format.replace(/hh/g, (hour < 12 ? hour : hour - 12).toString().padingStar(2, '0'));
            }
            if (/mm/.test(format)) {
                format = format.replace(/mm/g, this.getMinutes().toString().padStart(2, '0'));
            }
            if (/ss/.test(format)) {
                format = format.replace(/ss/g, this.getSeconds().toString().padStart(2, '0'));
            }
            return format;
        }

        //获取两个时间相隔的天数
        Date.prototype.betweenDays=function(date) {
            var daySpan = (Date.parse(this) - Date.parse(date)) / 86400000;
            return daySpan;
        }

github地址:https://github.com/gdoujkzz/JsDate.git

JS对时间的操作的更多相关文章

  1. js对时间的操作相关

    摘自网络,我主要用了日期增加若干天之后获得的日期,就是现在是5月2号,我增加30天,应该返回6月几号来着,就是这个意思 用到了Date.prototype.DateAdd 方法,prototype的意 ...

  2. js中时间的操作

    var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1 ...

  3. 关于JS的时间控制实现动态效果及实例操作

    关于JS的时间控制 <script>        BOM   //Bowers Object Model   浏览器对象模型    setTimeout()//    延迟执行一次   ...

  4. js格式化时间和时间操作

    js格式化时间 function formatDateTime(inputTime) { var date = new Date(inputTime); var y = date.getFullYea ...

  5. jsp+js完成用户一定时间未操作就跳到登录页面

    <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" ...

  6. js中常用的操作

    1.js中常用的数组操作 2.js中常用的字符串操作 3.js中常用的时间日期操作 4.定时器

  7. js的时间操作方法

    1.js获取系统时间格式为YYYY-MM-DD HH:MM:SS 1 function curDateTime(){ 2 var d = new Date(); 3 var year = d.getY ...

  8. Web页面长时间无操作后再获取焦点时转到登录界面

    今天开始讲新浪博客搬到博客园.        在工作中遇到的小问题,感觉有点意思,就记录下来吧!        该问题分为两种情况,一.Web页面长时间无操作后,在对其进行操作,比如点击“首页”.“设 ...

  9. Python3 与 C# 面向对象之~继承与多态 Python3 与 C# 面向对象之~封装 Python3 与 NetCore 基础语法对比(Function专栏) [C#]C#时间日期操作 [C#]C#中字符串的操作 [ASP.NET]NTKO插件使用常见问题 我对C#的认知。

    Python3 与 C# 面向对象之-继承与多态   文章汇总:https://www.cnblogs.com/dotnetcrazy/p/9160514.html 目录: 2.继承 ¶ 2.1.单继 ...

随机推荐

  1. HDU 1216 Assistance Required 埃拉托色尼色筛法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1216 思路:色筛法 代码(1): #include<iostream>//-------- ...

  2. 处理eclipse启动时报java.lang.IllegalStateException

    这是我写的第一篇博客,博客我来了: 我是好学的人,希望在这上面遇到志同道合的人,对技术有更高追求的人: 重启eclipse的时候报出来 An error has occurred, See the l ...

  3. 分享Sql Server 2008 r2 数据备份,同步服务器数据(一.本地备份)

    最近在部署一个系统,处于数据安全的考虑,因此对相应的数据库服务器定时备份,以及数据同步到备份服务上.之前在另外的一个项目中也做过相应的操作,但是操作都是按照查找到的文章一步一步的操作,碰到一些细节问题 ...

  4. tomcat线程初探

    博主:handsomecui,希望路过的各位大佬留下你们宝贵的意见,在这里祝大家冬至快乐. 缘由: 初探缘由,在业务层想要通过(当前线程的栈)来获取到控制层的类名,然后打日志,可是发现并不能通过当前线 ...

  5. uva11059(最大乘积)

    Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...

  6. [hdu 4869](14年多校I题)Turn the pokers 找规律+拓欧逆元

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. 【Android使用Shape绘制虚线,在4.0以上的手机显示实线】解决方式

    问题描写叙述: 用下面代码绘制虚线: <span style="font-family:Comic Sans MS;font-size:18px;"><? xml ...

  8. SourceTree 基本介绍

    Git的服务器端: 最出名的是GitHub,但是不能创建私有仓库,创建私有得需要Money Bitbucket:可以创建私有数据库,但是速度太慢,太消磨激情了 如果既想创建私有又想要激情,那只能自己搭 ...

  9. 面试题(php部分)

    1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分) [答案] $a = date("Y-m-d H:i:s", strtotime("-1 ...

  10. SpringBoot Test集成测试

    1.pom,文件添加相关依赖 如何测试SpringBoot的请求?使用spring-boot-starter-test这个包即可完成测试,SpringBoot项目为什么需要测试本章不作过多说明,重点放 ...