欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于jsDate时间转换毫秒中文自定义(代码教程)

程序员文章站 2022-04-15 21:25:54
关于jsdate时间转换毫秒中文自定义(代码教程) date.prototype.tolocalestring=function(){ var year=this.getfullyear(...

关于jsdate时间转换毫秒中文自定义(代码教程)

date.prototype.tolocalestring=function(){
  var year=this.getfullyear();
  if((this.getmonth()+1)<10){
    var month="0"+(this.getmonth() + 1);
  }
  if((this.getdate()<10)){
    var day="0"+this.getdate();
  }
  // return this.getfullyear() + "-" + (this.getmonth() + 1) + "-" + this.getdate();
  return year+'-'+month+'-'+day;
}

有些时间格式要求这样,可以在里面判断自己修改,这样定义就是:2010-01-01

date.prototype.tolocalestring = function() {
          return this.getfullyear() + "年" + (this.getmonth() + 1) + "月" + this.getdate() + "日 " + this.gethours() + "点" + this.getminutes() + "分" + this.getseconds() + "秒";
    };

2010年01月01日 12点12分12秒

new date.gettime()

把时间转换成毫秒数

var newdate=new date().gettime();

console.log(newdate);//把当前时间转换成毫秒

var oldtime = (new date("2012/12/25 20:11:11")).gettime(); //得到毫秒数

console.log(oldtime);

var oldtime = (new date("2012/12/25 20:11:11")).gettime(); //得到毫秒数

var newtime = new date(oldtime); //就得到普通的时间了

console.log(newtime.tolocalestring());//转换成中文