时间格式化方法

1 new Date()转换成2015-12-11 12:22:22格式的时间方法

//获得年月日 1970-01-18 07:12:39
function getMyDate(str){
    var oDate = new Date(str),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth()+1,
        oDay = oDate.getDate(),
        oHour = oDate.getHours(),
        oMin = oDate.getMinutes(),
        oSen = oDate.getSeconds(),
        oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间
    return oTime;
};
//补0操作
function getzf(num){
    if(parseInt(num) < 10){
        num = '0'+num;
    }
    return num;
}

2 . 毫秒值(时间戳)转换成自己想要的格式(扩展Date.prototype.toLocalString()方法)

得到后台从数据库中拿到的数据我们希望格式是2016年10月25日 17时37分30秒 或者 2016/10/25 17:37:30,然而我们前台得到的却是一段数字(时间戳,毫秒数)
1477386005,我们要将时间戳转化为我们想要的格式。

   var yestdayDate = $('#tradeStartDate').datebox('getValue');//2015-02-11
    var newDate = new Date(yestdayDate);  // Thu Feb 24 2017 08:00:00 GMT+0800 (中国标准时间)
    var yestMilc = newDate.getTime(); // 14121243356
    var cha = yestMilc -24*60*60*1000;//昨天的毫秒值
    var unixTimeStamp = new Date(cha); // Thu Feb 23 2017 08:00:00 GMT+0800 (中国标准时间)
    console.log(unixTimeStamp);
    var commontime = unixTimeStamp.toLocaleString();
    console.log("时间未:"+commontime);

//扩展toLocalString方法 格式 :2016年10月3日 2点3分3秒
Date.prototype.toLocaleString = function() {
          return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日 " + this.getHours() + "点" + this.getMinutes() + "分" + this.getSeconds() + "秒";
    };
//或者 :格式:2015/10/12 12:12:22
Date.prototype.toLocaleString = function() {
          return this.getFullYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate() + "/ " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
    };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值