1. [文件] yDate.js
/**
 * | yDate.js | Copyright (c) 2013 yao.yl | email: redrainyi@126.com | Date: 2012-09-03 |
 */
(function(global) {
 
    var objectPrototypeToString = Object.prototype.toString;
 
    var isDate = function(value) {
        return objectPrototypeToString.call(value) === '[object Date]';
    };
 
    var cloneDate = function(pDate, process) {
        var vDate = new Date(pDate.getTime());
        var year = vDate.getFullYear(), //
        month = vDate.getMonth(), //
        date = vDate.getDate(), //
        hours = vDate.getHours(), //
        minutes = vDate.getMinutes(), // 
        seconds = vDate.getSeconds();//
        (!!process) && process(vDate, year, month, date, hours, minutes, seconds);
        return vDate;
    };
 
    var parseDate = function(dateString, pattern) {
        try {
            var matchs1 = (pattern || (dateString.length === 10 ? 'yyyy-MM-dd' : 'yyyy-MM-dd HH:mm:ss')).match(/([yMdHsm])(\1*)/g);
            var matchs2 = dateString.match(/(\d)+/g);
            if (matchs1.length === matchs2.length) {
                var $d = new Date(1970, 0, 1);
                for (var i = 0; i < matchs1.length; i++) {
                    var $i = parseInt(matchs2[i], 10);
                    switch (matchs1[i].charAt(0) || '') {
                        case 'y' :
                            $d.setFullYear($i);
                            break;
                        case 'M' :
                            $d.setMonth($i - 1);
                            break;
                        case 'd' :
                            $d.setDate($i);
                            break;
                        case 'H' :
                            $d.setHours($i);
                            break;
                        case 'm' :
                            $d.setMinutes($i);
                            break;
                        case 's' :
                            $d.setSeconds($i);
                            break;
                        default :
                            //
                    }
                }
                return $d;
            }
        } catch (err) {
            alert(err)
        }
        return null;
    };
 
    var formatDate = (function() {
        var SIGN_RG = /([yMdHsm])(\1*)/g;
        function padding(s, len) {
            var len = len - (s + "").length;
            for (var i = 0; i < len; i++) {
                s = "0" + s;
            }
            return s;
        }
        return function(value, pattern) {
            if (!isDate(value)) {
                return '';
            }
            try {
                pattern = pattern || 'yyyy-MM-dd HH:mm:ss';
                return pattern.replace(SIGN_RG, function($0) {
                    switch ($0.charAt(0)) {
                        case 'y' :
                            return padding(value.getFullYear(), $0.length);
                        case 'M' :
                            return padding(value.getMonth() + 1, $0.length);
                        case 'd' :
                            return padding(value.getDate(), $0.length);
                        case 'w' :
                            return value.getDay() + 1;
                        case 'H' :
                            return padding(value.getHours(), $0.length);
                        case 'm' :
                            return padding(value.getMinutes(), $0.length);
                        case 's' :
                            return padding(value.getSeconds(), $0.length);
                        case 'q' :
                            return Math.floor((this.getMonth() + 3) / 3);
                        default :
                            return '';
                    }
                });
            } catch (err) {
                return '';
            }
        };
    })();
 
    var getActualMaximum = function(date) {
        var vDate = new Date(date.getTime());
        vDate.setMonth(vDate.getMonth() + 1);
        vDate.setDate(0);
        return vDate.getDate();
    }
 
    var YDate = function() {
        var p0 = arguments[0];
        var p1 = arguments[1];
        if (typeof p0 === 'number' && isFinite(value)) {
            this.vDate = new Date(p0);//millis
        } else if (isDate(p0)) {
            this.vDate = new Date(p0.getTime());
        } else if (typeof p0 === 'string') {
            if (typeof p1 === 'string' || typeof p1 === 'undefined') {
                this.vDate = parseDate(p0, p1);
            }
        } else if (arguments.length == 0) {
            this.vDate = new Date();
        } else {
            throw 'YDate Constructor Error!';
        }
        this.$year = this.vDate.getFullYear();
        this.$month = this.vDate.getMonth();
        this.$date = this.vDate.getDate();
        this.$hours = this.vDate.getHours();
        this.$minutes = this.vDate.getMinutes();
        this.$seconds = this.vDate.getSeconds();
        this.$day = this.vDate.getDay();
    };
 
    YDate.prototype = {
        plusYear : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setFullYear(year + value);
            }));
        },
        plusMonth : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setMonth(month + value);
            }));
        },
        plusDate : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setDate(date + value);
            }));
        },
        plusHours : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setHours(hours + value);
            }));
        },
        plusMinutes : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setMinutes(minutes + value);
            }));
        },
        plusSeconds : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setSeconds(seconds + value);
            }));
        },
        minusYear : function(value) {
            return this.plusYears(-value);
        },
        minusMonth : function(value) {
            return this.plusMonths(-value);
        },
        minusDate : function(value) {
            return this.plusDate(-value);
        },
        minusHours : function(value) {
            return this.plusHours(-value);
        },
        minusMinutes : function(value) {
            return this.plusMinutes(-value);
        },
        minusSeconds : function(value) {
            return this.plusSeconds(-value);
        },
        setYear : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setFullYear(value);
            }));
        },
        setMonth : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setMonth(value);http://www.huiyi8.com/clxgt/​
            }));窗帘效果图
        },
        setDate : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setDate(value);
            }));
        },
        setHours : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setHours(value);
            }));
        },
        setMinutes : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setMinutes(value);
            }));
        },
        setSeconds : function(value) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                vDate.setSeconds(value);
            }));
        },
        getYear : function() {
            return vDate.getFullYear();
        },
        getMonth : function() {
            return vDate.getMonth();
        },
        getDate : function() {
            return vDate.getDate();
        },
        getHours : function() {
            return vDate.getHours();
        },
        getMinutes : function() {
            return vDate.getMinutes();
        },
        getSeconds : function() {
            return vDate.getSeconds();
        },
        getDayOfWeek : function() {
            return vDate.getDay();
        },
        toDate : function() {
            return cloneDate(this.vDate);
        },
        calculate : function(expression) {
 
        },
        clone : function() {
            return new YDate(cloneDate(this.vDate));
        },
        getBegin : function(field) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                switch (field) {
                    case 'yyyy' ://year
                        vDate.setMonth(0);
                        vDate.setDate(1);
                        vDate.setHours(0);
                        vDate.setMinutes(0);
                        vDate.setSeconds(0);
                        break;
                    case 'MM' ://month
                        vDate.setDate(1);
                        vDate.setHours(0);
                        vDate.setMinutes(0);
                        vDate.setSeconds(0);
                    case 'dd' ://date
                        vDate.setHours(0);
                        vDate.setMinutes(0);
                        vDate.setSeconds(0);
                        break;
                    default :
                        //Ignore
                }
            }));
        },
        getEnd : function(field) {
            return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
                switch (field) {
                    case 'yyyy' ://year
                        vDate.setMonth(11);
                        vDate.setDate(31);
                        vDate.setHours(23);
                        vDate.setMinutes(59);
                        vDate.setSeconds(59);
                        break;
                    case 'MM' ://month
                        vDate.setDate(getActualMaximum(vDate));
                        vDate.setHours(23);
                        vDate.setMinutes(59);
                        vDate.setSeconds(59);
                    case 'dd' ://date
                        vDate.setHours(23);
                        vDate.setMinutes(59);
                        vDate.setSeconds(59);
                        break;
                    default :
                        //Ignore
                }
            }));
        },
        toString : function(pattern) {
            return formatDate(this.vDate, pattern);
        }
    };
    global.YDate = YDate;
})(window);
2. [代码]使用介绍

<!Doctype html>
<html>
    <head>
        <title>yDate.test</title>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
        <script type="text/javascript" src="yDate.js">
        </script>
    </head>
    <body>
        <script type="text/javascript">
 
//创建一个YDate日期对象
var date1 = new YDate('2013-01-01 11:50:20');
alert(date1.toString());
//获得JS Date对象
alert(date1.toDate());
 
var date2 = new YDate('2012-02-11');
alert(date2.toString());
//format日期对象
alert(date2.toString('yyyy年MM月dd日'));
 
//获得本月最后时刻的日期
var date3 = date2.getEnd('MM');//yyyy MM dd
alert(date3.toString());
 
//获得本年最初时刻的日期
var date4 = date2.getBegin('yyyy');
alert(date4.toString());
 
        </script>
    </body>
</html>

用于JS日期格式化,以及简单运算的Date包装工具类的更多相关文章

  1. 161226、js日期格式化

    JavaScript Date format(js日期格式化) 方法一:// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季 ...

  2. js日期格式化 扩展Date()

    javascript Date format(js日期格式化) 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(H/h).分(m).秒(s ...

  3. JS 日期格式化

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"& ...

  4. 【JavaScript】 knockout.js 日期格式化借助【momentjs】

    源:Knockout.js 日期格式化 源:momentjs

  5. JS获取当前日期时间及JS日期格式化

    Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...

  6. JS日期格式化(网上转载)

    JS日期格式化(网上转载) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <ht ...

  7. js 日期格式化函数(可自定义)

    js 日期格式化函数 DateFormat var DateFormat = function (datetime, formatStr) { var dat = datetime; var str ...

  8. 简单了解Spring中常用工具类_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...

  9. javascript常用开发笔记:一个简单强大的js日期格式化方法

    前言:一直没找到好用的javascript日期格式化工具,自己写了一个,方便以后复用 1.主要功能 (1)支持任意顺序的日期格式排列:yyyy=年,MM=月,dd=日,HH=时,mm=分,ss=秒,s ...

随机推荐

  1. Linux 下MySQL 安装与卸载

    这个写的比较好:http://www.cnblogs.com/starof/p/4680083.html 2.卸载系统自带的Mariadb rpm -qa|grep mariadb         / ...

  2. jquery知识汇总

    jQuery 选择器 选择器                  实例                                   选取 *                          $ ...

  3. Docker-PostgresSQL

    Postgresql  Docker安装运行 mac环境: 1.拉取官方镜像,并创建容器 zhoumatoMacBook-Pro:~ zhou$ docker search postgresql NA ...

  4. HDu1241 DFS搜索

    #include<iostream> #include<cstring> using namespace std; int a[105][105]; int d[8][2]={ ...

  5. android test控件

    1.Plain Text 输入文本框 <EditText android:id="@+id/editText" android:layout_width="wrap ...

  6. RabbitMQ 简介以及使用场景

    目录 一. RabbitMQ 简介 二. RabbitMQ 使用场景 1. 解耦(为面向服务的架构(SOA)提供基本的最终一致性实现) 2. 异步提升效率 3. 流量削峰 三. 引入消息队列的优缺点 ...

  7. php 求素数的二种方法

    <?php for($i = 2;$i <= 100;$i++) { for($j = 2; $j <= ($r = $i / $j); $j++) { if(($i % $j)== ...

  8. 基于centos 创建stress镜像——源码安装stress

    上一篇文章进行了yum安装stress,这次对stress进行源码编译安装,并且生成新的镜像 创建Dockerfile目录 [vagrant@localhost ~]$ mkdir -p /tmp/s ...

  9. RNN与LSTM

    Recurrent Neural Networks Recurrent neural networks are networks with loops in them, allowing inform ...

  10. 转: 环信联合创始人:App主流反垃圾服务难点和技术实现全解析

    转:http://science.china.com.cn/2016-03/24/content_8659834.htm 发布时间: 2016-03-24 13:15:02  |  来源: 全球财经网 ...