js将long日期格式转换为标准日期格式

来源:互联网 发布:淘宝主图软件下载 编辑:程序博客网 时间:2024/05/13 01:45
  1. <script language="javascript">     
  2.     //扩展Date的format方法   
  3.     Date.prototype.format = function (format) {  
  4.         var o = {  
  5.             "M+"this.getMonth() + 1,  
  6.             "d+"this.getDate(),  
  7.             "h+"this.getHours(),  
  8.             "m+"this.getMinutes(),  
  9.             "s+"this.getSeconds(),  
  10.             "q+": Math.floor((this.getMonth() + 3) / 3),  
  11.             "S"this.getMilliseconds()  
  12.         }  
  13.         if (/(y+)/.test(format)) {  
  14.             format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
  15.         }  
  16.         for (var k in o) {  
  17.             if (new RegExp("(" + k + ")").test(format)) {  
  18.                 format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));  
  19.             }  
  20.         }  
  21.         return format;  
  22.     }  
  23.     /**   
  24.     *转换日期对象为日期字符串   
  25.     * @param date 日期对象   
  26.     * @param isFull 是否为完整的日期数据,   
  27.     *               为true时, 格式如"2000-03-05 01:05:04"   
  28.     *               为false时, 格式如 "2000-03-05"   
  29.     * @return 符合要求的日期字符串   
  30.     */    
  31.     function getSmpFormatDate(date, isFull) {  
  32.         var pattern = "";  
  33.         if (isFull == true || isFull == undefined) {  
  34.             pattern = "yyyy-MM-dd hh:mm:ss";  
  35.         } else {  
  36.             pattern = "yyyy-MM-dd";  
  37.         }  
  38.         return getFormatDate(date, pattern);  
  39.     }  
  40.     /**   
  41.     *转换当前日期对象为日期字符串   
  42.     * @param date 日期对象   
  43.     * @param isFull 是否为完整的日期数据,   
  44.     *               为true时, 格式如"2000-03-05 01:05:04"   
  45.     *               为false时, 格式如 "2000-03-05"   
  46.     * @return 符合要求的日期字符串   
  47.     */    
  48.   
  49.     function getSmpFormatNowDate(isFull) {  
  50.         return getSmpFormatDate(new Date(), isFull);  
  51.     }  
  52.     /**   
  53.     *转换long值为日期字符串   
  54.     * @param l long值   
  55.     * @param isFull 是否为完整的日期数据,   
  56.     *               为true时, 格式如"2000-03-05 01:05:04"   
  57.     *               为false时, 格式如 "2000-03-05"   
  58.     * @return 符合要求的日期字符串   
  59.     */    
  60.   
  61.     function getSmpFormatDateByLong(l, isFull) {  
  62.         return getSmpFormatDate(new Date(l), isFull);  
  63.     }  
  64.     /**   
  65.     *转换long值为日期字符串   
  66.     * @param l long值   
  67.     * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss   
  68.     * @return 符合要求的日期字符串   
  69.     */    
  70.   
  71.     function getFormatDateByLong(l, pattern) {  
  72.         return getFormatDate(new Date(l), pattern);  
  73.     }  
  74.     /**   
  75.     *转换日期对象为日期字符串   
  76.     * @param l long值   
  77.     * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss   
  78.     * @return 符合要求的日期字符串   
  79.     */    
  80.     function getFormatDate(date, pattern) {  
  81.         if (date == undefined) {  
  82.             date = new Date();  
  83.         }  
  84.         if (pattern == undefined) {  
  85.             pattern = "yyyy-MM-dd hh:mm:ss";  
  86.         }  
  87.         return date.format(pattern);  
  88.     }  
  89.    
  90.     //alert(getSmpFormatDate(new Date(1279849429000), true));  
  91.     //alert(getSmpFormatDate(new Date(1279849429000),false));      
  92.     //alert(getSmpFormatDateByLong(1279829423000, true));  
  93.     alert(getSmpFormatDateByLong(1279829423000,false));      
  94.     //alert(getFormatDateByLong(1279829423000, "yyyy-MM"));  
  95.     //alert(getFormatDate(new Date(1279829423000), "yy-MM"));  
  96.     //alert(getFormatDateByLong(1279849429000, "yyyy-MM hh:mm"));       
  97. </script>  
0 0