JS/JQUERY(15)——JS中日期的各种转化

<script type="text/javascript">
            
            /**
             * 将时间转化为时间戳
             * 2015-03-02 12:06:29   转化为   1425269189
             * @param {Object} time
             */
            function transdate(time){ 
                var date=new Date(); 
                date.setFullYear(time.substring(0,4)); 
                date.setMonth(time.substring(5,7)-1); 
                date.setDate(time.substring(8,10)); 
                date.setHours(time.substring(11,13)); 
                date.setMinutes(time.substring(14,16)); 
                date.setSeconds(time.substring(17,19)); 
                return Date.parse(date)/1000; 
            } 
            
            function get_unix_time(dateStr){
                var newstr = dateStr.replace(/-/g,'/'); 
                var date =  new Date(newstr); 
                var time_str = date.getTime().toString();
                return time_str.substr(0, 10);
            }
            
//            alert(transdate("2015-03-02 12:06:29"));
//            alert(get_unix_time("2015-03-02 12:06:29"));
            
            
            /**
             * 2015-03-02 12:06:29 转化成:Mon Mar 02 12:06:29 CST 2015
             * date.toString()   : Mon Mar 2 12:06:29 UTC+0800 2015
             * date.toDateString()  : Mon Mar 2 2015
             * date.toLocaleDateString() : 2015年3月2日
             * date.toLocaleString()   : 2015年3月2日 12:06:29
             * date.toLocaleTimeString()  : 12:06:29
             * date.toGMTString()  : Mon, 2 Mar 2015 04:06:29 UTC
             * @param {Object} time
             */
            function transdatey(time){ 
                var date=new Date(); 
                date.setFullYear(time.substring(0,4)); 
                date.setMonth(time.substring(5,7)-1); 
                date.setDate(time.substring(8,10)); 
                date.setHours(time.substring(11,13)); 
                date.setMinutes(time.substring(14,16)); 
                date.setSeconds(time.substring(17,19)); 
                var str = date.toString();
                str = str.replace("UTC+0800", "CST");
                return str;
            } 
//            alert(transdatey(str));
            
            /**
             * 其中 GMT 与  UTC+0800对应的时间是相同的
             * 所以尽量采用  GMT  或   UTC+0800
             */
            var d = new Date('Mon Mar 02 12:06:29 CST 2015');
//            var d = new Date('Mon Mar 02 12:06:29 2015 GMT');
//            var d = new Date('Mon Mar 2 12:06:29 UTC+0800 2015');
            var sd = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
//            alert(sd);
            
            /**
             * 时间戳转化为时间
             * 1425269189  转化为  2015-03-02 12:06:29
             * @param {Object} tm
             */
            function getDate(tm){ 
//                var tt=new Date(parseInt(tm) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ") 
//                return tt; 
                return new Date(parseInt(tm) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
            } 
            function getLocalTime(nS) {     
               return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');     
            }     
            
//            alert(getDate("1425269189"));
//            alert(getLocalTime(1425269189));
            
            /**
             * 1425269189  转化为  2015年3月2日 12:06:29
             * @param {Object} tm
             */
            function getDatey(tm){ 
                var tt=new Date(parseInt(tm) * 1000).toLocaleString() 
                return tt; 
            } 
//            alert(getDatey("1425269189"));

            /**
             * d 日期 t 天数
             * @param {Object} d
             * @param {Object} t
             */
            function DateAdd(d, t) {
                var r = new Date(d.getTime() + (t * 24 * 60 * 60 * 1000));         
                 var m = r.getMonth() + 1;          
                var d = r.getDate();         
                 return r.getFullYear() + "-" + (m > 10 ? m : "0" + m) + "-" + (d > 10 ? d : "0" + d);//返回格式 2014-04-03
            }
            var date = new Date();
            var c = DateAdd(date,7); //加7天 
//            alert(c);


            /**
             * 对日期进行格式化
             * yyyy-MM-dd hh:mm:ss
             * @param {Object} format
             */
            Date.prototype.format = function(format) {
                var date = {
                      "M+": this.getMonth() + 1,
                      "d+": this.getDate(),
                      "h+": this.getHours(),
                      "m+": this.getMinutes(),
                      "s+": this.getSeconds(),
                      "q+": Math.floor((this.getMonth() + 3) / 3),
                      "S+": this.getMilliseconds()
                };
                if (/(y+)/i.test(format)) {
                      format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
                }
                for (var k in date) {
                      if (new RegExp("(" + k + ")").test(format)) {
                             format = format.replace(RegExp.$1, RegExp.$1.length == 1
                                    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
                      }
                }
                return format;
              }
            var newDate = new Date();
            alert(newDate.format('yyyy-MM-dd hh:mm:ss'));

                
            
            /**
             *  Date()    返回当日的日期和时间。
                getDate()    从 Date 对象返回一个月中的某一天 (1 ~ 31)。
                getDay()    从 Date 对象返回一周中的某一天 (0 ~ 6)。
                getMonth()    从 Date 对象返回月份 (0 ~ 11)。
                getFullYear()    从 Date 对象以四位数字返回年份。
                getYear()    请使用 getFullYear() 方法代替。
                getHours()    返回 Date 对象的小时 (0 ~ 23)。
                getMinutes()    返回 Date 对象的分钟 (0 ~ 59)。
                getSeconds()    返回 Date 对象的秒数 (0 ~ 59)。
                getMilliseconds()    返回 Date 对象的毫秒(0 ~ 999)。
                getTime()    返回 1970 年 1 月 1 日至今的毫秒数。
                getTimezoneOffset()    返回本地时间与格林威治标准时间 (GMT) 的分钟差。
                getUTCDate()    根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。
                getUTCDay()    根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。
                getUTCMonth()    根据世界时从 Date 对象返回月份 (0 ~ 11)。
                getUTCFullYear()    根据世界时从 Date 对象返回四位数的年份。
                getUTCHours()    根据世界时返回 Date 对象的小时 (0 ~ 23)。
                getUTCMinutes()    根据世界时返回 Date 对象的分钟 (0 ~ 59)。
                getUTCSeconds()    根据世界时返回 Date 对象的秒钟 (0 ~ 59)。
                getUTCMilliseconds()    根据世界时返回 Date 对象的毫秒(0 ~ 999)。
                parse()    返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
                setDate()    设置 Date 对象中月的某一天 (1 ~ 31)。
                setMonth()    设置 Date 对象中月份 (0 ~ 11)。
                setFullYear()    设置 Date 对象中的年份(四位数字)。
                setYear()    请使用 setFullYear() 方法代替。
                setHours()    设置 Date 对象中的小时 (0 ~ 23)。
                setMinutes()    设置 Date 对象中的分钟 (0 ~ 59)。
                setSeconds()    设置 Date 对象中的秒钟 (0 ~ 59)。
                setMilliseconds()    设置 Date 对象中的毫秒 (0 ~ 999)。
                setTime()    以毫秒设置 Date 对象。
                setUTCDate()    根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
                setUTCMonth()    根据世界时设置 Date 对象中的月份 (0 ~ 11)。
                setUTCFullYear()    根据世界时设置 Date 对象中的年份(四位数字)。
                setUTCHours()    根据世界时设置 Date 对象中的小时 (0 ~ 23)。
                setUTCMinutes()    根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
                setUTCSeconds()    根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。
                setUTCMilliseconds()    根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
                toSource()    返回该对象的源代码。
                toString()    把 Date 对象转换为字符串。
                toTimeString()    把 Date 对象的时间部分转换为字符串。
                toDateString()    把 Date 对象的日期部分转换为字符串。
                toGMTString()    请使用 toUTCString() 方法代替。
                toUTCString()    根据世界时,把 Date 对象转换为字符串。
                toLocaleString()    根据本地时间格式,把 Date 对象转换为字符串。
                toLocaleTimeString()    根据本地时间格式,把 Date 对象的时间部分转换为字符串。
                toLocaleDateString()    根据本地时间格式,把 Date 对象的日期部分转换为字符串。
                UTC()    根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。
                valueOf()    返回 Date 对象的原始值。
             */
            
            
            
        </script>

 

posted @ 2015-07-08 15:58  xu_shuyi  阅读(278)  评论(0)    收藏  举报