淘特cms
当前位置:淘特CMS->帮助中心->常见问题
java和asp程序,格式化日期的函数
  • 作者:
  • 日期:2016/4/3 17:03:23
  • 出处:淘特CMS
  • 点击:


因为两个项目都需要将日期字符格式为:2小时前,2天前这样的显示效果,因此分别制作 了两个函数。代码如下:

Asp日期格式函数:

  1. function FormTime(t)  
  2.                         dim ret  
  3.                         y=cint(t/(365*24*3600))  
  4.             if(y>0) then  
  5.                 ret=y&"年"  
  6.             else  
  7.                 m=cint(t/(30*3600*24))  
  8.                 if(m>0) then  
  9.                     ret=m&"月"  
  10.                 else  
  11.                     w=cint(t/(7*3600*24))  
  12.                     if(w>0) then  
  13.                         ret=w&"周"  
  14.                     else  
  15.                         d=cint(t/(3600*24))  
  16.                         if(d>0) then  
  17.                             ret=d&"天"  
  18.                         else  
  19.                             h=cint(t/3600)  
  20.                             if(h>0) then  
  21.                                 ret=h&"小时"  
  22.                             else  
  23.                                 mi=cint(t/60)  
  24.                                 if(mi>0) then  
  25.                                     ret=mi&"分钟"  
  26.                                 else  
  27.                                     ret="刚刚"  
  28.                                 end if  
  29.                             end if  
  30.                         end if  
  31.                     end if  
  32.                 end if  
  33.             end if  
  34.                         FormTime=ret  
  35.                     end function  


Java日期格式函数:


public static final long SECOND  = 1000;
    public static final long MINUTE  = SECOND * 60;
    public static final long HOUR    = MINUTE * 60;
    public static final long DAY     = HOUR * 24;
    public static final long WEEK    = DAY * 7;
    public static final long MONTH    = DAY * 30;
    public static final long YEAR    = DAY * 365;

  1. public static String FormTime(java.sql.Timestamp t){  
  2.         String ret="";  
  3.         if(t!=null){  
  4.             long now=System.currentTimeMillis();     
  5.             long st=t.getTime();  
  6.             long vers=(now-st);              
  7.             long y=vers/YEAR;  
  8.             if(y>0){  
  9.                 ret=y+"年";  
  10.             }else{  
  11.                 long m=vers/MONTH;  
  12.                 if(m>0){  
  13.                     ret=m+"月";  
  14.                 }else{  
  15.                     long w=vers/WEEK;  
  16.                     if(w>0){  
  17.                         ret=w+"周";  
  18.                     }else{  
  19.                         long d=vers/DAY;  
  20.                         if(d>0){  
  21.                             ret=d+"天";  
  22.                         }else{  
  23.                             long h=vers/HOUR;  
  24.                             if(h>0){  
  25.                                 ret=h+"小时";  
  26.                             }else{  
  27.                                 long mi=vers/MINUTE;  
  28.                                 if(mi>0){  
  29.                                     ret=mi+"分钟";  
  30.                                 }else{  
  31.                                     ret="刚刚";  
  32.                                 }  
  33.                             }  
  34.                         }  
  35.                     }  
  36.                 }  
  37.             }  
  38.         }  
  39.         return ret;  
  40.     }  

最新评论
用 户:
内 容:
验证码: