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. CCF-201503-3-节日

    问题描述 试题编号: 201503-3 试题名称: 节日 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 有一类节日的日期并不是固定的,而是以"a月的第b个星期c&q ...

  2. linux禁用锁定和解除解锁用户账号的方法

    Linux系统使用的是/etc/shadow保存加密了的用户密码,要禁止一个帐号的话,最快的方法就是修改存储于/etc/shadow中的密码. 一般情况下,一个有效的Linux用户在/etc/shad ...

  3. Chrome浏览器读写系统剪切板

    IE浏览器支持直接读写剪切板内容: window.clipboardData.clearData(); window.clipboardData.setData('Text', 'abcd'); 但是 ...

  4. Linux用户管理的复习时间

    所谓三天不练手生,你还记得关于Linux用户管理的所有知识吗?现在就来跟我一起复习一下吧! 1.常用配置文件 用户信息文件: /etc/password 密码文件: /etc/shadow 用户组文件 ...

  5. 初学sheel脚本练习过程

    以下是初学sheel脚本练习过程,涉及到内容的输出.基本的计算.条件判断(if.case).循环控制.数组的定义和使用.函数定义和使用 sheel脚本内容: #! /bin/bashecho &quo ...

  6. jar包冲突与inode

    包冲突 几乎上点规模的java系统就会遇到jar冲突,不负责任的讲排除依赖成了每次发布上线前必做的工作.虽然问题的本质都是jar冲突,但是表现上却有很多不同,从NoSuchMethodError,Cl ...

  7. JAVA通过Gearman实现MySQL到Redis的数据同步(异步复制)

    MySQL到Redis数据复制方案 无论MySQL还是Redis,自身都带有数据同步的机制,像比较常用的 MySQL的Master/Slave模式 ,就是由Slave端分析Master的binlog来 ...

  8. 编码中的setCharacterEncoding 理解<转自-fancychendong>

    1.pageEncoding="UTF-8"的作用是设置JSP编译成Servlet时使用的编码. 2.contentType="text/html;charset=UTF ...

  9. ArrayList与数组间的转换

    关键句:String[] array = (String[])list.toArray(new String[size]); public class Test { public static voi ...

  10. Oracle442个应用场景-----------角色管理

    --------------------------------角色管理------------------------------------ 一.角色的概念和特性 1.什么是角色? 角色就是相关权 ...