zoukankan      html  css  js  c++  java
  • js 时间与时间戳转换

    时间转换

    new Date 转换

      var date = new Date() //Mon Jul 13 2020 16:25:51 GMT+0800 (中国标准时间)
      date.getTime()  //1594628791783
    
      var date = new Date(1594628791783) //Mon Jul 13 2020 16:26:31 GMT+0800 (中国标准时间)
      date.getTime() //1594628791783
    
      var date = new Date("2020-02-02") // Sun Feb 02 2020 08:00:00 GMT+0800 (中国标准时间)
      var date = new Date("2020","02","02") // Sun Feb 02 2020 08:00:00 GMT+0800 (中国标准时间)
      var date = new Date("2020/02/02") //Sun Feb 02 2020 00:00:00 GMT+0800 (中国标准时间)
     
     eg: new Date("2020","02","02").getFullYear()
         new Date("2020","02","02").getMonth()
         ......
                
    

    日期的几种转换

      var d = new Date() //Object
      d.toString()  //"Mon Jul 13 2020 16:38:18 GMT+0800 (中国标准时间)"
      d.toDateString();  //转换成日期字符串 Mon Jul 13 2020
      d.toTimeString();  //转换成时间字符串 16:38:18 GMT+0800 (中国标准时间) 
      d.toLocaleDateString();   //返回本地的日期格式  (不同浏览器不同效果)2020/7/13
      d.toLocaleTimeString();   //返回本地的时间格式  (不同浏览器不同效果)下午4:38:18       
    

    获取时间部份

       // 类型均为 数字
      d.getSeconds()  //获取秒
      d.getMinutes()  //获取分钟
      d.getHours()    //获取小时
      d.getDay()      //返回周几   (0表示周日)
      d.getDate()     //返回当前月的第几天
      d.getMonth()    //返回月份   (从0开始)
      d.getFullYear()  //返回年份
    

    时间格式化

      Date.prototype.format = function (fmt) {
                var o = {
                    "M+": this.getMonth() + 1,                 //月份
                    "d+": this.getDate(),                    //日
                    "h+": this.getHours(),                   //小时
                    "m+": this.getMinutes(),                 //分
                    "s+": this.getSeconds(),                 //秒
                    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                    "S": this.getMilliseconds()             //毫秒
                };
                if (/(y+)/.test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  // 满足2002和02格式年份
                }
                for (var k in o) {
                    // M月d日和MM月dd日
                    if (new RegExp("(" + k + ")").test(fmt)) {
                        console.log(RegExp.$1.length == 1)
                        fmt = fmt.replace(
                            RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                    }
                }
                return fmt;
            }
           
         var now = new Date();
         var nowStr = now.format("yyyyy-MM-dd hh:mm:ss");  //2020-07-13 17:36:52
         var str = now.format("yy-MM-dd hh:mm:ss")         //20-07-13 17:49:15
         RegExp.$1  //是RegExp的一个属性,指的是与正则表达式匹配的第一个 子匹配(以括号为标志)字符串
    

    文档
    W3C

  • 相关阅读:
    使用Word发布文章到 WordPress 博客
    Wordpress上传到阿里云服务器
    IntelliJ设置鼠标悬浮提示和修改快捷键
    梅塔幻灯片如何设置图片高度不被裁减
    更改XAMPP中MySQL数据库的端口号
    PHP开启cURL功能
    Android Studio使用百度地图示例BaiduMapsApiASDemo
    CocosCreator反射在Android中的使用
    Android Studio新建一个HelloWorld 程序(App)
    无法中止进程无法访问操作拒绝访问
  • 原文地址:https://www.cnblogs.com/rainbowqq/p/13293837.html
Copyright © 2011-2022 走看看