首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

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

2012-05-22 
这个JS 日期格式化函数怎么用??Date.prototype.Format function(fmt){ //author: meizzvar o {M+ :

这个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");

热点排行