zoukankan      html  css  js  c++  java
  • 格式化日期和时间

    //格式化日期
    var months=["Jan", "Feb", "Mar", "Apr", "May","Jun","Jul","Aug","Sep","Oct","Nov", "Dec"];
    
    function formatDate(time){
    	if(!time){
    		return "";
    	}
    
    	var reg = /^(d{4})-(d{2})-(d{2})$/;
    	if(reg.test(time)){
    		return time;
    	}
    	if(typeof time=='number'){
    		var d=new Date(time);
    		var dStr = "";
    		var mStr = "";
    		if(d.getDate()<10){
    			dStr = "0"+d.getDate();
    		}else{
    			dStr = d.getDate();
    		}
    		if(d.getMonth()<9){ //month should +1
    			mStr = "0" + (d.getMonth()+1);
    		}else{
    			mStr = (d.getMonth()+1);
    		}
    		return d.getFullYear()+'-'+mStr+'-'+dStr;
    	}else{
    		var str=time.split('-');
    		if(str[1].indexOf('0')==0){
    			str[1]=str[1].substring(1);
    		}
    		return months[parseInt(str[1])-1]+" "+str[2]+" "+str[0];
    	}
    }
    
    
    //格式化日期时间
    function formatTime(time){
    	//debugger;
    	if(!time){
    		return "";
    	}
    	if(typeof time=='number'){
    		var d=new Date(time);
    		var dStr = "";
    		var mStr = "";
    		var hStr = "";
    		var miStr = "";
    		var seStr ="";
    		
    		if(d.getDate()<10){//日
    			dStr = "0"+d.getDate();
    		}else{
    			dStr = d.getDate();
    		}
    		if(d.getMonth()<9){ //month should +1
    			mStr = "0" + (d.getMonth()+1);
    		}else{
    			mStr = (d.getMonth()+1);
    		}
    		if(d.getHours()<10){
    			hStr="0"+(d.getHours()); 
    			
    		}else{
    			hStr=d.getHours();
    		}
    		if(d.getMinutes()<10){
    			miStr="0"+d.getMinutes();
    		}else{
    			miStr=d.getMinutes();
    		}
    		if(d.getSeconds()<10){
    			seStr="0"+d.getSeconds();
    		}else{
    			seStr=d.getSeconds();
    		}
    		return d.getFullYear()+'-'+mStr+'-'+dStr+' '+hStr+':'+miStr+':'+seStr;
    	}else{
    		var str=time.split('-');
    		if(str[1].indexOf('0')==0){
    			str[1]=str[1].substring(1);
    		}
    		return months[parseInt(str[1])-1]+" "+str[2]+" "+str[0];
    	}
    }
    

      

  • 相关阅读:
    三维几何模板
    凸包
    计算几何 部分模板
    几何模板
    高斯消元模板
    tarjin求割点
    在无向图中找最短桥(tarjan)
    线段树
    错排问题
    NABCD分析
  • 原文地址:https://www.cnblogs.com/bravolove/p/5523754.html
Copyright © 2011-2022 走看看