zoukankan      html  css  js  c++  java
  • JS中Date日期函数中的参数使用介绍,时间格式化

    view plainnew Date("month dd,yyyy hh:mm:ss"); 
    new Date("month dd,yyyy"); 
    new Date(yyyy,mth,dd,hh,mm,ss); 
    new Date(yyyy,mth,dd); 
    new Date(ms); 
    
    month:用英文表示月份名称,从January到December 
    mth:用整数表示月份,从0(1月)到11(12月) 
    Content 
    
    dd:表示一个月中的第几天,从1到31 
    yyyy:四位数表示的年份 
    hh:小时数,从0(午夜)到23(晚11点) 
    mm:分钟数,从0到59的整数 
    ss:秒数,从0到59的整数 
    ms:毫秒数,为大于等于0的整数,表示的是需要创建的时间和GMT时间1970年1月1日之间相差的毫秒数。
    

      可以支持 new Date("yyyy/MM/dd"); 其中:MM是整数表示月份从0(1月)到11(12月),这样再利用正则表达式就很方便地能够转换字符串日期了

    document.write("<br/>" + new Date("February 3,2009")); 
    document.write("<br/>" + new Date("February 3,2009 10:52:03")); 
    document.write("<br/>"); 
    document.write("<br/>" + new Date(2009,1,3)); 
    document.write("<br/>" + new Date(2009,1,3,10,52,03)); 
    document.write("<br/>"); 
    document.write("<br/>" + new Date(Date.parse("February 3,2009"))); 
    document.write("<br/>" + new Date(Date.parse("February 3,2009 10:52:03"))); 
    document.write("<br/>" + new Date(Date.parse(2009,1,3))); //Output: NAN 
    document.write("<br/>" + new Date(Date.parse(2009,1,3,10,52,03))); //Output: NAN 
    document.write("<br/>" + new Date(Date.parse("2009/02/03"))); 
    document.write("<br/>"); 
    document.write("<br/>" + new Date("2009/02/03")); 
    document.write("<br/>" + new Date("2009/02/03 11:12:13")); 
    document.write("<br/>" + new Date("2009-02-03")); 
    
    输出结果: 
    
    Tue Feb 3 00:00:00 UTC+0800 2009 
    Tue Feb 3 10:52:03 UTC+0800 2009 
    
    Tue Feb 3 00:00:00 UTC+0800 2009 
    Tue Feb 3 10:52:03 UTC+0800 2009 
    
    Tue Feb 3 00:00:00 UTC+0800 2009 
    Tue Feb 3 10:52:03 UTC+0800 2009 
    NaN 
    NaN 
    Tue Feb 3 00:00:00 UTC+0800 2009 
    
    Tue Feb 3 00:00:00 UTC+0800 2009 
    Tue Feb 3 11:12:13 UTC+0800 2009 
    NaN 
    // 时间格式化
            function dateFormat(date) {
                if (!date) {
                    return null;
                }
                date = date.toString().replace(/[D]/g, ""); // 清除时间除数字外字符
                var len = format.replace(/W/g, "").length; // 默认格式长度
                var str = date.length >= len ? date.slice(0, len) : '';
                if (date && str) {
                    switch (format) {
                        case 'yyyy-mm-dd hh:mm:ss':
                            date = str.replace(/(d{4})(d{2})(d{2})(d{2})(d{2})(d{2})/, "$1-$2-$3 $4:$5:$6");
                            break;
                        case 'yyyy-mm-dd hh:mm':
                            date = str.replace(/(d{4})(d{2})(d{2})(d{2})(d{2})/, "$1-$2-$3 $4:$5");
                            break;
                        case 'yyyy-mm-dd':
                            date = str.replace(/(d{4})(d{2})(d{2})/, "$1-$2-$3");
                            break;
                        case 'yyyy-mm':
                            date = str.replace(/(d{4})(d{2})/, "$1-$2");
                            break;
                        case 'yyyy':
                            date = str.replace(/(d{4})/, "$1");
                            break;
                        default:
                            break;
                    }return date;
                }
                return null;
            }
  • 相关阅读:
    JS标签获取另一个页面传过来的href值
    jsp/servlet实现简单上传和下载
    servlet跳转页面后图片不显示
    Nginx 配置实例-动静分离
    将博客搬至博客园
    nginx 配置实例-反向代理
    Nginx 简介与安装、常用的命令和配置文件
    nginx 配置实例-负载均衡
    nginx 配置实例-反向代理
    Nginx 简介与安装、常用的命令和配置文件
  • 原文地址:https://www.cnblogs.com/chenzxl/p/11016120.html
Copyright © 2011-2022 走看看