JavaScript日期时间格式化函数

本文介绍了一种在JavaScript中格式化日期的方法,并提供了一个实用的Date原型扩展函数。该函数支持多种日期格式,如年、月、日、时、分、秒及毫秒等,并附带测试代码验证其正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这篇文章主要介绍了JavaScript日期时间格式化函数分享,需要的朋友可以参考下

这个函数经常用到,分享给大家。

函数代码:

    //格式化参数说明:
    //y:年,M:月,d:日,h:时,m分,s:秒,t毫秒
    Date.prototype.Format = function(format){
    var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(),    //day
    "h+" : this.getHours(),   //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
    "t" : this.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
    (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,
    RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
}


<script type="text/javascript">
//测试代码
setInterval(function(){
    var time = new Date().format("hh:mm:ss.t");
    var date = new Date().format("yyyy-MM-dd");
    document.getElementById("Time").innerHTML=time;
    document.getElementById("Date").innerHTML=date;
},1000);

</script>
<div id="Date"></div>
<div id="Time"></div>

 

 出处:http://www.jb51.net/article/49726.htm

======================================================================

在使用上面的日期时间格式化函数的时候会出错,比如显示两个年份或月份等,所以最近又重新研究了下正则表达式的应用 ,所以重新把上面的代码升级了下,以下纯属自己研究,如有错误还请指正,代码如下:

<script language="JavaScript"> 
//格式化参数说明:
//y:年,M:月,d:日,k周,h:时,m分,s:秒,S毫秒
Date.prototype.Format = function(formatIn){
  var formatOut=formatIn;
  var o = {
  "M+" : this.getMonth()+1, //month
  "d+" : this.getDate(),    //day
  "h+" : this.getHours(),   //hour
  "m+" : this.getMinutes(), //minute
  "s+" : this.getSeconds(), //second
  "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
  "k+" : Math.ceil((Math.ceil((this.valueOf()-new Date(this.getFullYear(),0,1).valueOf())/86400000)+((new Date(this.getFullYear(),0,1).getDay()+1)-1))/7)        //weeks
  }
  if(/(y+)/ig.test(formatIn))
  for(var i=0;i<(formatIn.match(/(y+)/ig)).length;i++){formatOut=formatOut.replace((formatIn.match(/(y+)/ig))[i],(this.getFullYear()+"").substr(4 - (formatIn.match(/(y+)/ig))[i].length));}
  for(var k in o){
      var reg=new RegExp("("+ k +")","g");
    if(reg.test(formatIn))var t=formatIn.match(reg);
    for(var i in t)formatOut=formatOut.replace(t[i],(o[k]+"").length==1?("0"+ o[k]).substr(t[i].length>2?-t[i].length:2-t[i].length):o[k]);
  }
  if(/(S+)/g.test(formatIn))
  formatOut=formatOut.replace(formatIn.match(/(S+)/g)[0],(this.getMilliseconds()+"").length<2?("00"+this.getMilliseconds()).substr(0,formatIn.match(/(S+)/g)[0].length):((this.getMilliseconds()+"").length==2?("0"+this.getMilliseconds()).substr(0,formatIn.match(/(S+)/g)[0].length):(this.getMilliseconds()+"").substr(0,formatIn.match(/(S+)/g)[0].length)));
  return formatOut;
}
//测试代码    
var dt=new Date;
var f1="yyyy-MM-dd 第k周 hh:mm:ss.SS";
var f2="MM-dd kkk yy-M-d k h:m:s.S";
var f3="hh:mm:ss.SSSS";
alert(dt.Format(f1));
alert(dt.Format(f2));

</script> 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值