zoukankan      html  css  js  c++  java
  • JS---- 时间格式化

    javascript 时间格式化

    有的时候,我们需要一定格式的 时间 比如 2017-05-12 08:48 这样的格式。 
    上代码先

    时间格式化

    第一种

    function formatDate(time){
        var date = new Date(time);
    
        var year = date.getFullYear(),
            month = date.getMonth() + 1,//月份是从0开始的
            day = date.getDate(),
            hour = date.getHours(),
            min = date.getMinutes(),
            sec = date.getSeconds();
        var newTime = year + '-' +
                    month + '-' +
                    day + ' ' +
                    hour + ':' +
                    min + ':' +
                    sec;
        return newTime;         
    }

    输出结果: 
    输出结果

    前置0

    但是这里存在一个问题,就是,我想要的格式应该是 2017-05-12 08:49:25 在月、日、时、分、秒 小于10的时候,应该要前置一个0。 
    改进代码:

    第二种

    function formatDate(time){
        var date = new Date(time);
    
        var year = date.getFullYear(),
            month = date.getMonth()+1,//月份是从0开始的
            day = date.getDate(),
            hour = date.getHours(),
            min = date.getMinutes(),
            sec = date.getSeconds();
        var newTime = year + '-' +
                    (month < 10? '0' + month : month) + '-' +
                    (day < 10? '0' + day : day) + ' ' +
                    (hour < 10? '0' + hour : hour) + ':' +
                    (min < 10? '0' + min : min) + ':' +
                    (sec < 10? '0' + sec : sec);
    
        return newTime;         
    }
    
    formatDate(new Date().getTime());//2017-05-12 09:09:21

    第三种

    这下格式对了。但是会不会麻烦了点?我们再试试这种 
    一个长度为10 的数组:

    var preArr = Array.apply(null,Array(10)).map(function(elem, index) {
            return '0'+index;
        });////开个长度为10的数组 格式为 ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09"]

    在如果数字在preArr中则群preArr[i],否则 就本身的值,比如:preArr[month]||month。具体实现如下:

    function formatDate(time){
        var date = new Date(time);
    
        var year = date.getFullYear(),
            month = date.getMonth()+1,//月份是从0开始的
            day = date.getDate(),
            hour = date.getHours(),
            min = date.getMinutes(),
            sec = date.getSeconds();
        var preArr = Array.apply(null,Array(10)).map(function(elem, index) {
            return '0'+index;
        });////开个长度为10的数组 格式为 00 01 02 03
    
        var newTime = year + '-' +
                    (preArr[month]||month) + '-' +
                    (preArr[day]||day) + ' ' +
                    (preArr[hour]||hour) + ':' +
                    (preArr[min]||min) + ':' +
                    (preArr[sec]||sec);
    
        return newTime;         
    }
    formatDate(new Date().getTime());//2017-05-12 09:45:41

    任意设置时间模式

    第四种(推荐)

    以上,都是按照固定的格式YY-MM-DD hh:mm:ss 输出的。要是产品突然说,改成2017年05月12这种格式,天啦撸,又要改o(╯□╰)o。那我还是写个结构好一些的吧,你们随便玩。

    function formatDate(time,format='YY-MM-DD hh:mm:ss'){
        var date = new Date(time);
    
        var year = date.getFullYear(),
            month = date.getMonth()+1,//月份是从0开始的
            day = date.getDate(),
            hour = date.getHours(),
            min = date.getMinutes(),
            sec = date.getSeconds();
        var preArr = Array.apply(null,Array(10)).map(function(elem, index) {
            return '0'+index;
        });////开个长度为10的数组 格式为 00 01 02 03
    
        var newTime = format.replace(/YY/g,year)
                            .replace(/MM/g,preArr[month]||month)
                            .replace(/DD/g,preArr[day]||day)
                            .replace(/hh/g,preArr[hour]||hour)
                            .replace(/mm/g,preArr[min]||min)
                            .replace(/ss/g,preArr[sec]||sec);
    
        return newTime;         
    }
    formatDate(new Date().getTime());//2017-05-12 10:05:44
    formatDate(new Date().getTime(),'YY年MM月DD日');//2017年05月12日
    formatDate(new Date().getTime(),'今天是YY/MM/DD hh:mm:ss');//今天是2017/05/12 10:07:45
  • 相关阅读:
    .Net 开源项目资源大全
    无法向会话状态服务器发出会话状态请求
    网站限制不能点击右键
    ashx页面中context.Session["xxx"]获取不到值的解决办法
    SQL Server 2008 错误 233 的解决办法
    Best MVC Practices 最佳的MVC实践
    char* 转换成 CString
    MFC圆角背景移动边角底色毛刺解决方案
    CString转换为const char*
    分享一份安卓开发包,集成开发环境
  • 原文地址:https://www.cnblogs.com/liaohongwei/p/9317195.html
Copyright © 2011-2022 走看看