• js 日期new date();格式转换,参数问题


    1.new Date()传参有多种形式

    avaScript下,new Date([params]),参数传递有以下五种方式:

    1、new Date("month dd,yyyy hh:mm:ss");

    2、new Date("month dd,yyyy");

    3、new Date(yyyy,mth,dd,hh,mm,ss); 注意:这种方式下,必须传递整型;

    4、new Date(yyyy,mth,dd);

    5、new Date(ms); 注意:ms:是需要创建的时间和 GMT时间1970年1月1日之间相差的毫秒数;当前时间与GMT1970.1.1之间的毫秒数:var mills = new Date().getTime();

    各种变量代表的含义是:

    month:用英文 表示月份名称,从January到December ,缩写也行(Jan…Dec);

    mth:用整数表示月份,从0(1月)到11(12月)

    dd:表示一个 月中的第几天,从1到31

    yyyy:四位数表示的年份

    hh:小时数,从0(午夜)到23(晚11点)

    mm: 分钟数,从0到59的整数

    ss:秒数,从0到59的整数

    ms:毫秒数,为大于等于0的整数

    举个栗子:

    new Date("Jun 2,2017 12:00:00"); //Fri Jun 02 2017 12:00:00 GMT+0800 (中国标准时间)

    new Date("Jun 2,2017"); //Fri Jun 02 2017 00:00:00 GMT+0800 (中国标准时间)

    new Date(2017,5,2,12,0,0); //Fri Jun 02 2017 12:00:00 GMT+0800 (中国标准时间)

    new Date(2017,5,2); //Fri Jun 02 2017 00:00:00 GMT+0800 (中国标准时间)

    new Date(1496376000000); //Fri Jun 02 2017 12:00:00 GMT+0800 (中国标准时间)

    以上输出的都是2017年6月2号的时间

    2.格式转换

    将日期时间转换为指定格式,如:YYYY-mm-dd HH:MM表示2019-06-06 19:45
    
    function dateFormat(fmt, date) {
        let ret;
        const opt = {
            "Y+": date.getFullYear().toString(),        //
            "m+": (date.getMonth() + 1).toString(),     //
            "d+": date.getDate().toString(),            //
            "H+": date.getHours().toString(),           //
            "M+": date.getMinutes().toString(),         //
            "S+": date.getSeconds().toString()          //// 有其他格式化字符需求可以继续添加,必须转化成字符串
        };
        for (let k in opt) {
            ret = new RegExp("(" + k + ")").exec(fmt);
            if (ret) {
                fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
            };
        };
        return fmt;
    }
    

    let date = new Date()
    dateFormat("YYYY-mm-dd HH:MM", date)
    >>> 2019-06-06 19:45`
     

    3.

    var myDate = new Date();//获取系统当前时间
      
    1 myDate.getYear(); //获取当前年份(2位)
     2 myDate.getFullYear(); //获取完整的年份(4位,1970-????)
     3 myDate.getMonth(); //获取当前月份(0-11,0代表1月)
     4 myDate.getDate(); //获取当前日(1-31)
     5 myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
     6 myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
     7 myDate.getHours(); //获取当前小时数(0-23)
     8 myDate.getMinutes(); //获取当前分钟数(0-59)
     9 myDate.getSeconds(); //获取当前秒数(0-59)
    10 myDate.getMilliseconds(); //获取当前毫秒数(0-999)
    11 myDate.toLocaleDateString(); //获取当前日期
    12 var mytime=myDate.toLocaleTimeString(); //获取当前时间
    13 myDate.toLocaleString( ); //获取日期与时间

    在 iOS 系统上初始化组件失败?

    如果你遇到了在 iOS 上无法渲染组件的问题,请确认在创建 Date 对象时没有使用new Date('2020-01-01')这样的写法,iOS 不支持以中划线分隔的日期格式,正确写法是new Date('2020/01/01')

    对此问题的详细解释:stackoverflow

    转载自:https://blog.csdn.net/Aiyining/article/details/87925443

    转载自:https://www.jianshu.com/p/49fb78bca621

  • 相关阅读:
    职场之道
    AlphaBlend
    感动前行——给医学媳妇写的演讲稿(非IT类)
    高等数学积分公式大全
    分析Model2系统心得
    【软考】(六)关系代数
    飞鸽传书官方站点 创立黑马程序猿训练营
    实现简单的二级级联
    const和readonly差别
    Chord算法(原理)
  • 原文地址:https://www.cnblogs.com/ygyy/p/13044220.html
Copyright © 2020-2023  润新知