• js new Date()参数格式


    最近在写页面使用new Date()获取时间戳在ie浏览器中测试发现无效;后来发现是参数格式问题,

    new Date()参数格式如下:

    1、用整数初始化日期对象 
    var date1 = new Date(2017,06,06); console.log(date1); // Thu Jul 06 2017 00:00:00 GMT+0800 (中国标准时间) 
    var date1 = new Date(2017,1,1); console.log(date1); // Wed Feb 01 2017 00:00:00 GMT+0800 (中国标准时间) 
    var date1 = new Date(2017,01-2,01); console.log(date1); // Thu Dec 01 2016 00:00:00 GMT+0800 (中国标准时间) 
    var date1 =new Date(2017,06,06,06,06,06); console.log(date1); // Thu Jul 06 2017 06:06:06 GMT+0800 (中国标准时间) 
    说明: new Date( year, month, date, hrs, min, sec) 按给定的参数创建一日期对象

    2、用字符串初始化日期对象 
    var date2 = new Date(“2017/06/06”); console.log(date2); // Tue Jun 06 2017 00:00:00 GMT+0800 (中国标准时间) 
    var date2 = new Date(“2017-08-08”); console.log(date2); // Tue Aug 08 2017 08:00:00 GMT+0800 (中国标准时间) 
    var date2 = new Date(“2017-9-9”); console.log(date2); // Sat Sep 09 2017 00:00:00 GMT+0800 (中国标准时间) 
    说明:如果字符串模式不支持短横杠模式,则进行字符串替换: 
    var strTime=”2011-04-16”; 
    var date2= new Date(Date.parse(strTime.replace(/-/g, “/”))); // /-/g为正则表达式(RegExp) 对象,表示全局替换-为/。

    3、用毫秒时间戳初始化日期对象 
    var timestamp=new Date().getTime(); console.log( new Date(timestamp) ); //Tue Jun 06 2017 11:06:59 GMT+0800 (中国标准时间) 
    var date3 = new Date( timestamp - 1 * 60 * 60 * 1000); console.log(date3); // Tue Jun 06 2017 10:06:59 GMT+0800 (中国标准时间) 
    说明:时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。时间戳唯一地标识某一刻的时间。

    以上是在http://www.php.cn/js-tutorial-389248.html复制过来的

    以下是个人踩得坑:

    1、在IE浏览器中不支持格式“2017-08-08”,需要转换为“2017/08/08”

    后续继续踩坑。。。。。。

  • 相关阅读:
    一套完整的3D结构光系统搭建指南!
    寒武纪refindet caffe模型开始的步骤
    solidworks怎么在圆柱体上添加文字
    CVPR 2022 | 南开程明明团队和天大提出LD:目标检测的定位蒸馏
    实例 | 博途S71200 和V20变频器的MODBUS通讯
    CVPR 2022 | 南开程明明团队和天大提出LD:目标检测的定位蒸馏
    spacevim: 从入门到放弃
    每周分享
    No module named ipython
    maven项目中 jar包下载失败的解决:
  • 原文地址:https://www.cnblogs.com/hongll/p/10483856.html
Copyright © 2020-2023  润新知