js Date 时间转换 毫秒 中文 自定义

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());//转换成中文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值