JavaScript 格式化时间 INT类型 转 字符串时间

格式化时间
参数: formatStr 格式化串 y年,m月,d日,h小时,i分钟,s秒钟  缺省值 "y-m-d h:i:s"
      fdate 要格式化的时间(时间戳)UTC秒数 缺省值 当前时间
实例: formatDate()  当前时间默认格式 如 2011-4-12 12:51:12
      formatDate('y/m/d', 2132132131) 某时间格式为 年月日 如 2010/12/5  
date.getYear();        //获取当前年份(2位)
date.getFullYear();    //获取完整的年份(4位,1970-????)
date.getMonth();       //获取当前月份(0-11,0代表1月)
date.getDate();        //获取当前日(1-31)
date.getDay();         //获取当前星期X(0-6,0代表星期天)
date.getTime();        //获取当前时间(从1970.1.1开始的毫秒数)
date.getHours();       //获取当前小时数(0-23)
date.getMinutes();     //获取当前分钟数(0-59)
date.getSeconds();     //获取当前秒数(0-59)
date.getMilliseconds();    //获取当前毫秒数(0-999)
date.toLocaleDateString();     //获取当前日期
var mytime=date.toLocaleTimeString();     //获取当前时间
date.toLocaleString( );        //获取日期与时间
function formatDate(formatStr, fdate)
{
 var fTime, fStr = 'ymdhis';
 if (!formatStr)
  formatStr= "y-m-d h:i:s";
 if (fdate)
  fTime = new Date(fdate);
 else
  fTime = new Date();
 var formatArr = [
 fTime.getFullYear().toString(),
 (fTime.getMonth()+1).toString(),
 fTime.getDate().toString(),
 fTime.getHours().toString(),
 fTime.getMinutes().toString(),
 fTime.getSeconds().toString() 
 ]
 for (var i=0; i<formatArr.length; i++)
 {
  formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
 }
 return formatStr;
}
时间转毫秒
//时间转毫秒
function c1(data){
var b = (new Date(data)).getTime();
return b;
}
//显示:当前日期加时间(如:2009-06-12 12:00)
function CurentTime()
    { 
        var now = new Date();
       
        var year = now.getFullYear();       //年
        var month = now.getMonth() + 1;     //月
        var day = now.getDate();            //日
       
        var hh = now.getHours();            //时
        var mm = now.getMinutes();          //分
       
        var clock = year + "-";
       
        if(month < 10)
            clock += "0";
       
        clock += month + "-";
       
        if(day < 10)
            clock += "0";
           
        clock += day + " ";
       
        if(hh < 10)
            clock += "0";
           
        clock += hh + ":";
        if (mm < 10) clock += '0'; 
        clock += mm; 
        return(clock); 
    } 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值