美文网首页
js时间对象的格式化

js时间对象的格式化

作者: 小呆糊总 | 来源:发表于2019-03-20 15:52 被阅读0次
var myDate = new Date();//返回:Wed Mar 20 2019 15:15:11 GMT+0800 (中国标准时间)
var mytime=myDate.format('yyyy-MM-dd');//返回:2019-03-20
/*如果是一个后台获取的下拉框,每个下拉框包含数据startData是一个
时间戳,把时间戳转化成2019-03-20格式*/
<select name="food" >
    <option value="pingguo" index="0">苹果</option>
    <option value="li" index="1">梨</option>
    <option value="tao" index="2">桃子</option>
    <option value="you" index="3">柚子</option>
</select>
//后台接口返回的json
{
  "status" : true,
  "message" : null,
  "data" : [ {
    "id" : 6,
    "name" : "苹果",
    "startDate" : 1552579200000,
  }, {
    "id" : 6,
    "name" : "梨",
    "startDate" : 1552579300000,
  }, {
    "id" : 6,
    "name" : "桃子",
    "startDate" : 1552578200000,
  }, {
    "id" : 6,
    "name" : "柚子",
    "startDate" : 1552579800000,
  }]
}
//下拉列表属性index为接口返回的数据data[i]的i值
var index=$(this).find("option:selected").attr('index');//1552579200000
var myDate = new Date(response.data[index].startDate);
var payDate=myDate.format('yyyy-MM-dd');//2019-03-20
 // 时间对象的格式化; 
Date.prototype.format = function (format) {
    /* 
     * eg:format="yyyy-MM-dd hh:mm:ss"; 
     */
    var o = {
        "M+": this.getMonth() + 1, // month  
        "d+": this.getDate(), // day  
        "h+": this.getHours(), // hour  
        "m+": this.getMinutes(), // minute  
        "s+": this.getSeconds(), // second  
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter  
        "S": this.getMilliseconds()
        // millisecond  
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

相关文章

  • JS 一些解决方案记录

    时间相关 时间格式化 Unix时间戳转换 JS 对象相关 利用 json 转换进行对象深拷贝 JSON.strin...

  • js时间对象的格式化

  • js公共函数总结

    字节大小转换 Object 对象的深度克隆(深拷贝) 时间格式化 js利用空对象作为中介实现继承 需求:get请求...

  • js Date对象详解

    1、建立时间对象:可获取年,月,日,星期,时,分,秒 2、获取时间戳 3、时间转换公式 4、js格式化日期

  • js时间格式化

    js时间格式化代码贴上:

  • IntelliJ IDEA 自定义配置

    配置 JS 的一行对象里空格默认对JS代码的对象格式化不符合ESLint要求,需要前后各加一空格。修改JS代码空格...

  • JS时间戳与格式化时间互转

    JS时间戳与格式化时间互转 Javascript 获取当前时间戳(毫秒级别) 时间戳转成格式化时间 格式化时间转成...

  • 【第61天】python全栈从入门到放弃

    1.time模块 1-1 时间戳,时间对象,格式化时间相互转化 1-2 格式化时间,转为时间对象,转为时间戳 2 ...

  • js时间格式化

    照抄element-ui的时间格式化,将当前时间或者自定义时间格式化为自己需要的格式 dateFormat.js ...

  • js日期格式化

    js时间戳日期格式化 mongo脚本写法 字符串转时间戳

网友评论

      本文标题:js时间对象的格式化

      本文链接:https://www.haomeiwen.com/subject/okazmqtx.html