zoukankan      html  css  js  c++  java
  • js/jq基础(日常整理记录)-1-纯js格式化时间

    一、纯js格式化时间

     之前记录了一些,工作中发现的比较常用的使用,就记录一下。

    由于很基础,就直接贴出来了,不做分析了。

    改造一下Date的原型

    Date.prototype.format = function(format){ 
        var o = { 
        "M+" : this.getMonth()+1, //month 
        "d+" : this.getDate(), //day 
        "h+" : this.getHours(), //hour 
        "m+" : this.getMinutes(), //minute 
        "s+" : this.getSeconds(), //second 
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter 
        "S" : this.getMilliseconds() //millisecond 
        } 
    
        if(/(y+)/.test(format)) { 
        format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
        } 
    
        for(var k in o) { 
            if(new RegExp("("+ k +")").test(format)) { 
                format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); 
            } 
        } 
        return format; 
    }  

     看看如何使用吧。 

    //使用方法 
    var now = new Date(); 
    var nowStr = now.format("yyyy-MM-dd hh:mm:ss"); 
    
    //使用方法2: 
    var testDate = new Date(); 
    var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒"); 
    alert(testStr); 
    //示例: 
    console.log(new Date().format("yyyy年MM月dd日"));
    console.log(new Date().format("MM/dd/yyyy"));
    console.log(new Date().format("yyyyMMdd"));
    console.log(new Date().format("yyyy-MM-dd hh:mm:ss"));

    看看执行的结果吧。

    还是蛮好用的吧,把这段简短的js代码放置到公共的js文件中,以后直接使用就可以啦,就不用自己去转化了哈。

  • 相关阅读:
    6、scala面向对象-对象
    C# App.config配置文件的讲解
    abstract、override、new、virtual、sealed使用和示例
    C# 枚举的使用
    深入浅出面向对象分析与设计
    数据契约(DataContract)的作用
    C# 启动停止SQLServer数据库服务器
    C# 定时器计划任务
    C# 程序只能执行一次
    WPF dataGrid中的check的改变事件
  • 原文地址:https://www.cnblogs.com/newwind/p/8920469.html
Copyright © 2011-2022 走看看