当前位置: 代码迷 >> J# >> 这个JS 日期格式化函数如何用?
  详细解决方案

这个JS 日期格式化函数如何用?

热度:2341   发布时间:2013-02-25 00:00:00.0
这个JS 日期格式化函数怎么用??
Date.prototype.Format = function(fmt)  
{ //author: meizz  
  var o = {  
  "M+" : this.getMonth()+1, //月份  
  "d+" : this.getDate(), //日  
  "h+" : this.getHours(), //小时  
  "m+" : this.getMinutes(), //分  
  "s+" : this.getSeconds(), //秒  
  "q+" : Math.floor((this.getMonth()+3)/3), //季度  
  "S" : this.getMilliseconds() //毫秒  
  };  
  if(/(y+)/.test(fmt))  
  fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));  
  for(var k in o)  
  if(new RegExp("("+ k +")").test(fmt))  
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));  
  return fmt;  


下面是调用
  var time1 = new Date().Format("yyyy-MM-dd");
  alert(time1);

我得出的是当前时间。怎么把他改成格式化,我从数据库读出来的 字段?

------解决方案--------------------------------------------------------
var d = new Date("数据库字段内容写这里,如")
var d = new Date("2012/12/12")
time1 = d.Format("yyyy-MM-dd");
  相关解决方案