js格式化日期

日期类型:/Date(1406822400000)/

[javascript]  view plain  copy
  1. function extend_Todecimal(str, count) {  
  2.     if (str==undefined || str==""||str==null||str==NaN) {  
  3.         return 0;  
  4.     }  
  5.     var vv = Math.pow(10, count);  
  6.     return Math.round(str * vv) / vv;  
  7. }  
  8. Date.prototype.format = function (format) {  
  9.     var o = {  
  10.         "M+"this.getMonth() + 1, //month   
  11.         "d+"this.getDate(),    //day   
  12.         "h+"this.getHours(),   //hour   
  13.         "m+"this.getMinutes(), //minute   
  14.         "s+"this.getSeconds(), //second   
  15.         "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter   
  16.         "S"this.getMilliseconds() //millisecond   
  17.     }  
  18.     if (/(y+)/.test(format)) format = format.replace(RegExp.$1,  
  19.     (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
  20.     for (var k in o) if (new RegExp("(" + k + ")").test(format))  
  21.         format = format.replace(RegExp.$1,  
  22.       RegExp.$1.length == 1 ? o[k] :  
  23.         ("00" + o[k]).substr(("" + o[k]).length));  
  24.     return format;  
  25. }  
  26. function extend_formatDateTime(value) {  
  27.     if (value == null || value == '') {  
  28.         return '';  
  29.     }  
  30.     var dt;  
  31.     if (value instanceof Date) {  
  32.         dt = value;  
  33.     }  
  34.     else {  
  35.         dt = new Date(value);  
  36.         if (isNaN(dt)) {  
  37.             value = value.replace(/\/Date (?\d+) \//, '$1');  
  38.             dt = new Date();  
  39.             dt.setTime(value);  
  40.         }  
  41.     }  
  42.     return dt.format("yyyy-MM-dd hh:mm");  
  43. }  
  44. function extend_formatDate(value) {  
  45.     if (value == null || value == '') {  
  46.         return '';  
  47.     }  
  48.     var dt;  
  49.     if (value instanceof Date) {  
  50.         dt = value;  
  51.     }  
  52.     else {  
  53.         dt = new Date(value);  
  54.         if (isNaN(dt)) {  
  55.             value = value.replace(/\/Date (?\d+) \//, '$1');  
  56.             dt = new Date();  
  57.             dt.setTime(value);  
  58.         }  
  59.     }  
  60.     return dt.format("yyyy-MM-dd");  
  61. }  
  62.   
  63.   
  64. function extend_formatDateMmDd(value) {  
  65.     if (value == null || value == '') {  
  66.         return '';  
  67.     }  
  68.     var dt;  
  69.     if (value instanceof Date) {  
  70.         dt = value;  
  71.     }  
  72.     else {  
  73.         dt = new Date(value);  
  74.         if (isNaN(dt)) {  
  75.             value = value.replace(/\/Date (?\d+) \//, '$1');  
  76.             dt = new Date();  
  77.             dt.setTime(value);  
  78.         }  
  79.     }  
  80.     return dt.format("MM-dd");  
  81. }  
  82.   
  83.   
  84. $.extend(  
  85.     $.fn.datagrid.defaults.editors, {  
  86.         my97: {  
  87.         init: function (container, options) {  
  88.             var input = $('<input class="Wdate" onclick="WdatePicker({dateFmt:\'yyyy-MM-dd\',readOnly:true});"  />').appendTo(container);  
  89.             return input;  
  90.         },  
  91.         getValue: function (target) {  
  92.             return $(target).val();  
  93.         },  
  94.         setValue: function (target, value) {  
  95.             $(target).val(extend_formatDate(value));  
  96.         },  
  97.         resize: function (target, width) {  
  98.             var input = $(target);  
  99.             if ($.boxModel == true) {  
  100.                 input.width(width - (input.outerWidth() - input.width()));  
  101.             } else {  
  102.                 input.width(width);  
  103.             }  
  104.         }  
  105.     },  
  106.         datebox: {  
  107.             init: function (container, options) {  
  108.                 var input = $('<input type="text">').appendTo(container);  
  109.                 input.datebox(options);  
  110.                 return input;  
  111.             },  
  112.             destroy: function (target) {  
  113.                 $(target).datebox('destroy');  
  114.             },  
  115.             getValue: function (target) {  
  116.                 return $(target).datebox('getValue');  
  117.             },  
  118.             setValue: function (target, value) {  
  119.                 $(target).datebox('setValue', formatDatebox(value));  
  120.             },  
  121.             resize: function (target, width) {  
  122.                 $(target).datebox('resize', width);  
  123.             }  
  124.         }  
  125.     },  
  126.     {  
  127.         extend_getContentByType: function (type) {  
  128.             if (type == "加载")  
  129.                 return "数据正在加载中...";  
  130.             else if (type == "无数据")  
  131.                 return "没有找到相关数据!";  
  132.         },  
  133.         extend_extend_getUrlVars: function () {  
  134.             var vars = [], hash;  
  135.             var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');  
  136.             for (var i = 0; i < hashes.length; i++) {  
  137.                 hash = hashes[i].split('=');  
  138.                 vars.push(hash[0]);  
  139.                 vars[hash[0]] = hash[1];  
  140.             }  
  141.             return vars;  
  142.         },  
  143.         extend_getUrlVar: function (name) {  
  144.             return $.extend_extend_getUrlVars()[name];  
  145.         },  
  146.         extend_getCheckBoxValues: function (checkName) {  
  147.             var weeks = '';  
  148.             var $sel = $("input[name='" + checkName + "']:checked");  
  149.             if ($sel.length > 0) {  
  150.                 var selectedItems = new Array();  
  151.                 $sel.each(function () { selectedItems.push($(this).val()); });  
  152.                 weeks = selectedItems.join(',')  
  153.             }  
  154.             return weeks;  
  155.         },  
  156.         extend_formatterdatebytype: function (val, type) {  
  157.             var NewDtime = new Date(parseInt(val.replace("/Date(""").replace(")/"""), 10));  
  158.             var Dyear = NewDtime.getFullYear();  
  159.             var Dmonth = NewDtime.getMonth() + 1;  
  160.             var Ddate = NewDtime.getDate();  
  161.             // NewDtime.getMonth() + 1, //month   
  162.             // NewDtime.getDate(),    //day   
  163.             //NewDtime.getHours(),   //hour   
  164.             //NewDtime.getMinutes(), //minute   
  165.             //NewDtime.getSeconds(), //second   
  166.             //Math.floor((NewDtime.getMonth() + 3) / 3),  //quarter   
  167.             //NewDtime.getMilliseconds() //millisecond    
  168.             try {  
  169.                 return type.replace("yyyy", Dyear).replace("MM", Dmonth).replace("dd", Ddate);  
  170.             } catch (e) {  
  171.                 return Dyear + "-" + Dmonth + "-" + Ddate;  
  172.             }  
  173.         }  
  174.     }  
  175. );  

使用:

extend_formatDateMmDd(a.BeginDate) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值