Date.prototype.pattern=function(fmt) {
//alert(this.getFullYear());
fmt=fmt.toUpperCase();
var o = {
"MM" : this.getMonth(), //月份
"DD" : this.getDate(), //日
"HH24" : this.getHours(), //小时
"HH" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
"MI" : this.getMinutes(), //分
"SSS" : this.getMilliseconds(), //毫秒
"SS" : this.getSeconds(), //秒
"QQ" : Math.floor((this.getMonth()+3)/3), //季度
"YYYY":this.getFullYear()<1911?this.getFullYear()+1911:this.getFullYear(), //國際元年
"YYY":this.getFullYear()>1911?this.getFullYear()-1911:this.getFullYear() //台灣民國年
};
//if(/(y+)/.test(fmt)){
// fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
//}
//if(/(E+)/.test(fmt)){
// fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);
//}
for(var k in o){
if(new RegExp("("+ k +")").test(fmt)){
var needLength=RegExp.$1=="HH24"?2:RegExp.$1.length;
//alert("RegExp.$1:"+k +" o[k]:"+o[k].toString().length+" :"+(RegExp.$1.length==o[k].toString().length)+" >"+("00"+ o[k]).substr(-needLength));
fmt = fmt.replace(RegExp.$1, (needLength==o[k].toString().length) ? (o[k]) : (("00"+ o[k]).substr(-needLength)));
}
}
return fmt;
};

String.prototype.toDate=function(){
var year=0,month=0,day=0,hour=0,min=0,sec,mill=0;
var dateflag=false,timeflag=false;
var _value = this.replace(/(^\s*)|(\s*$)/g,"");
//console.log(_value);

var arr=_value.split(/\s/);
if(arr[0])
{
dateflag=true;

var _date=arr[0];
do
{
_date=_date.replace(/[-\/]/,"");

}while(_date.match(/[-\/]/));
if(_date.length!=7&&_date.length!=8)
{
throw 'get the date info is error, ther value is:'+_date;
}
console.log(arr[0]);
year=_date.substring(0,_date.length-4);
month=_date.substring(_date.length-4,_date.length-2);
day=_date.substr(_date.length-2);
console.log("year:"+year +" month:"+month+" day:"+day);

}

if(arr[1])
{
timeflag=true;
console.log(arr[1]);

var _time=arr[1].split(":",3);

hour=_time[0];
min=_time[1];
if(_time[2].indexOf('.')>-1)
{
sec=_time[2].split('.')[0];
mill=_time[2].split('.')[1];
}
else
{
sec=_time[2];
}

console.log("hour:"+hour +" min:"+min+" sec:"+sec+" mill:"+mill);
}

if(dateflag&&!timeflag)
{
var myDate=new Date();
myDate.setFullYear(year);
myDate.setMonth(month);
myDate.setDate(day);
return myDate;
}
else if(dateflag&&timeflag)
{
var myDate=new Date(year,month,day,hour,min,sec);
myDate.setFullYear(year);
myDate.setMonth(month);
myDate.setDate(day);
myDate.setHours(hour);
myDate.setMinutes(min);
myDate.setSeconds(sec);
myDate.setMilliseconds(mill);
return myDate;
}
else
{
throw 'conver string to date have error';
}
}

//var myDate="107-01-01 12:01:01".toDate().pattern();
var myDate="1999-01-01 23:03:04";
document.write(myDate+"<br />")
document.write(myDate.toDate().pattern("YYY-MM-DD HH:MI:SS"));

</script>

最新文章

  1. Microsoft Dynamics CRM 2013 的相关更新 2013-12
  2. Fragment全解析系列(二):正确的使用姿势
  3. 扩展progress_timer的计时精度
  4. 基本变换(读书笔记5 --- Real-Time rendering)
  5. VS2010 支持 CSS3
  6. 四则运算(Android)版
  7. 平衡二叉树,AVL树之图解篇
  8. python中的binascii
  9. ReadingNotes@02-12-2013
  10. 使用Fiddler抓取手机上的数据包
  11. js 实现二叉树
  12. mac 辅助接口
  13. java获取当前应用的运行信息(内存,线程,运行时间,状态等)
  14. 设计模式 --&gt; (6)原型模式
  15. Python内置函数(54)——callable
  16. DUEditor的诞生
  17. 特殊计数序列——第二类斯特林(stirling)数
  18. 在本地运行正常的静态网页放到tomcat中却显示异常的原因
  19. 在CentOS下的docker容器中部署spring boot应用的两种方式
  20. POJ 3905 Perfect Election (2-Sat)

热门文章

  1. bzoj 1814 Ural 1519 Formula 1 ——插头DP
  2. [转]boost::python开发环境搭建
  3. springboot+mockito 异常解决方案
  4. Win7系统安装Centos7.0双系统(一)
  5. eclipse开发Java web工程时,jsp第一行报错,如何解决?
  6. Master公式计算递归时间复杂度
  7. 一、CSS介绍
  8. scrapy-logging
  9. Linux 关机、重启 命令
  10. leetcode238